如何使用 requestHeader 向端点发出 post/get 请求?

时间:2021-03-12 12:10:17

标签: spring spring-boot rest spring-webclient

有问题的方法

@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 标头?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

正如@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();
        }
    }