以下是HttpRequestExecutingMessageHandler
的配置@ServiceActivator(inputChannel = "rtpRequestChannel")
@Bean
public MessageHandler httResponseMessageHandler(MessageChannel rtpResponseChannel) {
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(
"http://localhost:8080/rtp");
handler.setHttpMethod(HttpMethod.POST);
handler.setOutputChannel(rtpResponseChannel);
handler.setShouldTrack(true);
handler.setStatsEnabled(true);
return handler;
}
以下是REST控制器类中的POST方法:
@RequestMapping(value = "/rtp", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<RTPResponse> persistRTP(@RequestBody RTPRequest request) {
System.out.println("In post method " + request);
if (request != null) {
return new ResponseEntity<RTPResponse>(new RTPResponse("12:12:2017", "Test", null, "100", "100"), HttpStatus.OK);
}
return new ResponseEntity<RTPResponse>(new RTPResponse("12:12:2017", "Dummy", null, "Dummy", "Dummy"), HttpStatus.OK);
}
以下是服务激活方法的配置:
@Override
@ServiceActivator(inputChannel="rtpResponseChannel")
public void makeCall(ResponseEntity<RTPResponse> message) {
System.out.println("Message: " + message.getBody());
System.out.println(message.getClass().getCanonicalName());
}
我在ResponseEntity对象的主体中收到null。我错过了哪种配置?
修改1:
当我使用setExpectedResponseType()
时,使用与上面相同的控制器配置。
@ServiceActivator(inputChannel = "rtpRequestPostOperationRequestChannel")
@Bean
public MessageHandler httResponseMessageHandler(MessageChannel rtpRequestPostOperationResponseChannel) {
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(
"http://localhost:8080/rtp");
handler.setHttpMethod(HttpMethod.POST);
handler.setOutputChannel(rtpRequestPostOperationResponseChannel);
handler.setExpectedResponseType(RTPResponse.class);
return handler;
}
RTPResponse
对象未包含在ResponseEntity
中。
我收到如下错误:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method makeCall(rtp.model.RTPResponse) cannot be found on rtp.RTPRequestServiceClient type
编辑2:
换句话说,我应该在HttpRequestExecutingMessageHandler
上使用什么配置来获取消息对象,以便我在消息有效负载中拥有提取的主体以及MessageHeaders
的所有标头,包括状态。
我尝试将GenericMessage
传递给setExpectedResponseType
类的HttpRequestExecutingMessageHandler
方法。
但它给了我如下的错误,这是可以理解的:
Can not construct instance of org.springframework.messaging.support.GenericMessage: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
答案 0 :(得分:2)
但你说自己 - setExpectedResponseType()
。
你真的很想念这个配置。
在这种情况下,响应实体的body
为空:
private class ResponseEntityResponseExtractor<T> implements ResponseExtractor<ResponseEntity<T>> {
@Nullable
private final HttpMessageConverterExtractor<T> delegate;
public ResponseEntityResponseExtractor(@Nullable Type responseType) {
if (responseType != null && Void.class != responseType) {
this.delegate = new HttpMessageConverterExtractor<>(responseType, getMessageConverters(), logger);
}
else {
this.delegate = null;
}
}
@Override
public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
if (this.delegate != null) {
T body = this.delegate.extractData(response);
return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body);
}
else {
return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).build();
}
}
}
如果您不想为该选项提供Class<?>
,可以考虑使用:
/**
* Specify the {@link Expression} to determine the type for the expected response
* The returned value of the expression could be an instance of {@link Class} or
* {@link String} representing a fully qualified class name.
* @param expectedResponseTypeExpression The expected response type expression.
* Also see {@link #setExpectedResponseType}
*/
public void setExpectedResponseTypeExpression(Expression expectedResponseTypeExpression) {
代替。在这种情况下,您真的可以针对requestMessage
解析目标预期响应类型,并且还可以访问整个BeanFactory
以进行其他一些bean调用。