提问人:pintekus 提问时间:10/25/2023 最后编辑:vandenchpintekus 更新时间:10/26/2023 访问量:23
具有不同正文和响应 JSON 的 Java WebClient 发布请求
Java WebClient post request with different body and response JSON
问:
我正在尝试使用 Spring Boot Webclient 库创建 POST 请求。但是,输入正文与响应不同,并且响应不包含错误。
private Mono<ApplicationView> createMarketplaceApp(boolean published, String name, Type type) {
WebClient webClient = WebClient.create("url");
AppCreationRequest appCreationRequest =
new AppCreationRequest(published, name, type);
Mono<ApplicationView> newApp = webClient
.post()
.uri("/api/v1/apps")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("X-Auth-Token", "xxx-yyy-zzz")
.body(Mono.just(appCreationRequest), AppCreationRequest.class)
.exchangeToMono(response -> {
if (response.statusCode().equals(HttpStatus.OK)) {
return response.bodyToMono(ApplicationView.class);
} else {
// Turn to error
return response.createException().flatMap(Mono::error);
}
});
return newApp;
}
@Getter
@Setter
@ToString
@NoArgsConstructor
@QueryEntity
@Document
public class ApplicationView {
private String id;
private String type;
private String displayName;
private Boolean everPublished;
private Boolean publicDisabled;
private List<String> workIn;
private Map<String, Object> ext;
}
@Data
public class AppCreationRequest {
@NotNull
private Boolean published;
@Size(min = 1, max = 50)
private String name;
@NotNull
private Type type;
}
不知道为什么它不踩线
if (response.statusCode().equals(HttpStatus.OK)) {
我应该在 newApp 返回对象中期待什么?
答: 暂无答案
评论