![](/img/trans.png)
[英]To add custom header I used HttpServletRequestWrapper that header i dont get in @RequestHeader HttpHeader httpHeader but in request its there
[英]How do I make a post / get request to a endpoint with a requestHeader?
有问题的方法
@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 header?
您只需要设置正确的内容类型。 如果您的 controller 期望它是“纯文本”,您可能必须在请求客户端中明确设置它。 415 确实表示未匹配。
正如@Alex 所提到的,您是自动装配构建器,而不是寻找 WebClient 的具体实现。 请检查我的 WebClient 配置 bean。 但这不是实际问题。
当您使用 webClient 发送正文时,您必须使用
.body(...)
因此,要发送 controller 期望纯文本正文的纯文本正文,您需要以下内容:
.body(BodyInserters.fromProducer(Mono.just("random body"), String.class))
当 controller 要求 object 时,您需要使用类似这样的东西
.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;
}
}
Web客户端的配置
@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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.