【问题标题】:How do I make a post / get request to a endpoint with a requestHeader?如何使用 requestHeader 向端点发出 post/get 请求?
【发布时间】:2021-06-10 11:00:12
【问题描述】:

有问题的方法

@GetMapping("/all")
public Mono<ResponseEntity<String>> getSomeData(@RequestHeader String someId) {
  ...some code
}

尝试使用此方法调用消费端点:

@Autowired
WebClient.Builder webClient;
String someString = webClient.
  .get()
  .uri(someUrl)
  .header("someId", "someString")
  .retrieve()
  .bodyToMono(String.class)
  .block();

我的状态为 415,媒体类型不受支持,“不支持内容类型”

如何使用 webClientBuilder 设置我的 id 标头?

【问题讨论】:

  • 你能发布你的 WebClient 的完整配置吗?您在这里自动装配构建器。这里有一些不完整的信息。
  • Webclient 完整配置?我在这里使用弹簧靴。我假设当我在这里注入它时它是为我自动配置的。 @Alexander Katsenelenbogen

标签: spring spring-boot rest spring-webclient


【解决方案1】:

您只需要设置正确的内容类型。如果您的控制器希望它是“纯文本”,您可能必须在请求的客户端中明确设置它。 415 确实表示未匹配。

【讨论】:

    【解决方案2】:

    正如@Alex 所提到的,您是自动装配构建器,而不是寻找 WebClient 的具体实现。请检查我的 WebClient 配置 bean。但这不是真正的问题。

    当您使用 webClient 发送正文时,您必须使用

    .body(...)
    

    因此,要在控制器期望纯文本的地方发送纯文本正文,您需要以下内容:

    .body(BodyInserters.fromProducer(Mono.just("random body"), String.class))
    

    当控制器请求一个对象时,你需要使用类似的东西

     .body(BodyInserters.fromProducer(Mono.just(new Greet("Hello there this is the body of post request")), Greet.class))
    

    问候.java

    public static class Greet {
            String name;
    
            public Greet() {
            }
    
            public Greet(String name) {
                this.name = name;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
        }
    

    WebCLient 的配置

    @Configuration
        class WebClientConfig {
            @Bean
            WebClient webClient() {
                return WebClient.builder().baseUrl("http://localhost:8080/").build();
            }
        }
    
    @RequestMapping("/sample")
        @RestController
        static class SampleComntroller {
    
            private final WebClient webClient;
    
            @Autowired
            SampleComntroller(WebClient webClient) {
                this.webClient = webClient;
            }
    
            @GetMapping(value = "/main-get")//, consumes = MediaType.APPLICATION_JSON_VALUE)
            public Mono<String> helloGet(@RequestHeader(name = "someId") String someId) {
                return Mono.just("Hello, Spring!, get, response with header is=>" + someId);
            }
    
            @PostMapping(value = "/main-post-plain-string", consumes = MediaType.TEXT_PLAIN_VALUE)
            public Mono<String> helloPost(@RequestHeader(name = "someId") String someId, @RequestBody String body) {
                return Mono.just("Hello, Spring!, post, response with header is=>" + someId + " and random body " + UUID.randomUUID().toString());
            }
    
            @PostMapping(value = "/main-post-object", consumes = MediaType.APPLICATION_JSON_VALUE)
            public Mono<String> helloPostObject(@RequestHeader(name = "someId") String someId, @RequestBody Greet greet) {
                return Mono.just("Hello, Spring!, post, response with header is=>" + someId + " " + greet.getName() + " " + UUID.randomUUID().toString());
            }
    
            @GetMapping("/delegate-get")
            public String delegateGet() {
                return webClient
                        .get()
                        .uri("/sample/main-get")
                        .header("someId", "178A-0E88-get")
                        .retrieve().bodyToMono(String.class).block();
            }
    
            @PostMapping("/delegate-post")
            public String delegatePost() {
                return webClient
                        .post()
                        .uri("/sample/main-post-plain-string")
                        .body(BodyInserters.fromProducer(Mono.just("random body"), String.class))
                        .header("someId", "178A-0E88-post")
                        .retrieve()
                        .bodyToMono(String.class).block();
            }
    
            @PostMapping("/delegate-post-object")
            public String delegatePostObject() {
                return webClient
                        .post()
                        .uri("/sample/main-post-object")
                        .body(BodyInserters.fromProducer(Mono.just(new Greet("Hello there this is the body of post request")), Greet.class))
                        .header("someId", "178A-0E88-post")
                        .retrieve()
                        .bodyToMono(String.class).block();
            }
        }
    

    【讨论】:

    • 在哪里可以找到您的 webclient config bean?
    • 我的答案中有请检查。
    猜你喜欢
    • 2019-01-13
    • 2017-05-05
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2021-11-17
    • 1970-01-01
    • 2020-01-13
    相关资源
    最近更新 更多