nairavs:
Webクライアントの次の実装を持っています:
public <T> WebClient.ResponseSpec sendRequest(HttpMethod method, String contentType, T body, String baseUrl, String path) {
try {
WebClient webClient = WebClient.builder().baseUrl(baseUrl).filter(logRequest()).build();
WebClient.ResponseSpec responseSpec = webClient.method(method)
.uri(path)
.header(HttpHeaders.CONTENT_TYPE, contentType)
.body(BodyInserters.fromObject(body))
.retrieve();
return responseSpec;
} catch (Exception e) {
throw new WebClientProcessingException("Exception when trying to execute request", e);
}
}
// This method returns filter function which will log request data
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
LOGGER.info("Request: {} {} {}", clientRequest.method(), clientRequest.url(), clientRequest.body());
clientRequest.headers().forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
return Mono.just(clientRequest);
});
}
また、その要求を送信するためにWebクライアントを呼び出して、ユーザーオブジェクトが含まれているユーザーオブジェクトやコマンドを作成して、次のコードを、持っています
@Autowired
private BaseWebClient baseWebClient;
@Override
public void saveOrUpdateUser() {
UserPayload userPayload = new UserPayload();
userPayload.setUserId(111L);
userPayload.setCreatedAt(ZonedDateTime.now(DateTimeProps.systemTimeZone));
UserCommand userCommand = new UserCommand();
userCommand.setUser(userPayload);
baseWebClient.sendRequest(HttpMethod.POST, "application/json",
Stream.of(userCommand).collect(Collectors.toList()),
"http://localhost:8080",
"/users").onStatus(HttpStatus::isError, clientResponse -> {
throw new WebClientUserSaveOrUpdateeFailedException("Exception when trying to update user state")
.bodyToMono(String.class);
});
}
ユーザーペイロード:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserPayload {
Long userId;
ZonedDateTime createdAt;
}
Userコマンド:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserCommand {
@JsonProperty("user")
UserPayload user;
}
私の他のアプリ(私は要求を送信しています)を待っているJSON:
[
{ "user":
{
"userId": 1,
"createdAt": "2019-05-16T08:24:46.412Z"
}
}
]
使い方:私は何も起こりませんリクエストを送信しようとしているときに春は、(ゲッター/セッターのための)ロンボク、Gradleの2を起動します。さえも例外ではありません。私はよく、同じ問題として非常に単純なケースで試してみました。もう一つのノート、それは身体をログに記録することは可能でしょうか?私は何とか私は一般的な何かをしないのです推測最終JSONを参照してください意味します。
パンダ:
原子炉では、あなたが購読するまで何も起こりません。retrive()
実際には、要求を開始しません。あなたが見ることができるようにたとえば、あなたが出版社にResponseSpecを変換するための方法のいずれかを使用して、最終的にはその出版社に加入する必要があります。
あなたは、このメソッドを使用している方法によっては、春ではなく、出版社に加入しましょうすることができるかもしれません。WebFluxは、あなたができることを意味したモデルで、反応の種類をサポートし、直接モノを返す例えば、あなたのRestController方法から。