代码之家  ›  专栏  ›  技术社区  ›  KnowledgePath

springframework.web.client RestClient是否处理@requestParam[重复]

  •  0
  • KnowledgePath  · 技术社区  · 1 年前

    我试着这样做:

    @GetMapping("/db/video/getall")
    public ResponseEntity<List<VideoDto>> getAllVideo(@RequestParam String ownerEmail) {        
            return ....;
        }
    
    import org.springframework.web.client.RestClient;
    
    RestClient restClient = RestClient.create("http://localhost:8095");
    
    List<VideoDto> result = restClient.get()
        .uri("/db/video/getall")
        .accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .body(new ParameterizedTypeReference<List<VideoDto>>() {});
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>6.1.2</version>
      </dependency>
    

    但我找不到显示RestClient如何将ownerMail作为@RequestParam传递的代码示例。

    我在网上找到了@PathVariable代码示例。

    那么,RestClient处理@RequestParam吗?

    1 回复  |  直到 1 年前
        1
  •  0
  •   Unmitigated    1 年前

    对于GET请求,可以在URL中添加查询参数。春天的 UriComponentsBuilder 简化了URI的操作。

    restClient.get()
        .uri(UriComponentsBuilder.fromPath("/db/video/getall")
                .queryParam("ownerEmail", "email").toUriString())
        // ...
    

    对于POST请求,可以将主体设置为 MultiValueMap 。可以根据内容来推断内容类型。

    var parts = new LinkedMultiValueMap<String, Object>();
    parts.add("ownerEmail", "emailValue");
    restClient.post().uri("/db/video/getall").body(parts).retrieve().body(...);