有我的代碼。
public Mono<RespDto> function(TransReqDto1 reqDto1, TransReqDto2 reqDto2, String token) {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("TransReqDto1", reqDto1);
builder.part("TransReqDto2", reqDto2);
MultiValueMap<String, HttpEntity<?>> parts = builder.build();
LinkedMultiValueMap map = new LinkedMultiValueMap();
map.add("TransReqDto1", reqDto1);
map.add("TransReqDto2", reqDto2);
return
client.post()
.uri("/api")
.body(BodyInserters.fromValue(reqDto1))
.headers(h -> h.setBearerAuth(token.split(" ")[1]))
.retrieve()
.bodyToMono(RespDto.class);
}
我的問題是我需要同時發送 reqDto1 和 reqDto2。我已經使用上面的代碼成功發送了 reqDto1,但我想不出發送兩個物件的方法。嘗試了 MultipartBodybuild 和 MultiValueMap,但都從目標 API 回傳錯誤。請給我一些提示!!謝謝
這是我要呼叫的 API!
@PostMapping("")
@ApiOperation(value = "test", notes = "test")
public Mono<?> transPost(@Valid @RequestBody TransReqDto1 reqDto1,
@Valid @RequestBody TransReqDto2 reqDto2) {
return testService.function(reqDto1, reqDto2);
}
uj5u.com熱心網友回復:
您不能使用兩個@RequestBody. 它只能系結到單個物件。預期的方法是創建一個包含所有相關資料的包裝器 DTO:
public class TransReqDto {
private TransReqDto1 transReqDto1;
private TransReqDto2 transReqDto2;
//...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360710.html
