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

w41d8nur  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(455)

有问题的方法

@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,媒体类型不受支持,内容类型“”不受支持
如何使用WebClient Builder设置id头?

cnwbcb6i

cnwbcb6i1#

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

2o7dmzc5

2o7dmzc52#

正如@alex提到的,您是autowiringbuilder,而不是寻找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();
        }
    }

相关问题