下午好,
我正在使用出站网关来调用可以使用(非 SOAP)XML 并生成(非 SOAP)XML 的服务。我可以编组 JAXB 类来请求 XML,但无法将响应 XML 解组回 JAXB 类,有效负载正文为 null。
流程如下
.subFlowMapping("SomeRequestType", subflow -> subflow
.transform(someRequestTransformer)
.enrichHeaders(header -> header.header("Content-Type","application/xml"))
.handle(someServiceOutboundGateway)
.transform(someResponseTransformer)
)
从someServiceOutboundGateway
@Bean
public HttpMessageConverter m() {
MarshallingHttpMessageConverter c = new MarshallingHttpMessageConverter();
c.setMarshaller(someMarshaller());
c.setUnmarshaller(someUnMarshaller());
return c;
}
@Bean(name="someServiceOutboundGateway")
public MessageHandler someOutboundGateway() {
return Http.outboundGateway(someUrl, lnquiHttp())
.httpMethod(HttpMethod.POST)
.expectedResponseType(Response.class)
.get();
}
@Bean
public RestTemplate lnquiHttp() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(connections);
connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute);
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(timeout) // timeout to get connection from pool
.setSocketTimeout(timeout) // standard connection timeout
.setConnectTimeout(timeout) // standard connection timeout
.build();
HttpClient httpClient = HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
RestTemplate restTemplate = restTemplateBuilder
.requestFactory(requestFactory)
.basicAuthorization(userName, password)
.messageConverters(m())
.build();
return restTemplate;
}
@Bean
public Marshaller someMarshaller() {
final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(CONTEXTPATH_REQUEST);
marshaller.setSchema(responseSchema);
marshaller.setSupportJaxbElementClass(Boolean.TRUE);
return marshaller;
}
@Bean
public Unmarshaller someUnMarshaller() {
final Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
unmarshaller.setContextPath(CONTEXTPATH_RESPONSE);
unmarshaller.setSchema(responseSchema);
unmarshaller.setSupportJaxbElementClass(Boolean.TRUE);
return unmarshaller;
}
@Bean(name = "someJAXBContext")
public JAXBContext someJAXBContext() throws JAXBException {
return JAXBContext.newInstance(Response.class);
}
从someResponseTransformer
@Component
public class SomeResponseTransformer implements GenericHandler<Object> {
@Override
public Object handle(final Object payload, final Map<String, Object> headers) {
ResponseEntity responseEntity = (ResponseEntity)payload;
Object body = responseEntity.getBody();
Response lnqiResponse = (Response)body;
... = buildHeader(lnqiResponse.getHeader());
当我尝试获取标头 ( lnqiResponse.getHeader()
) 时,我收到 NullPointerException。
知道如何解组对 的响应Response
吗?任何帮助,将不胜感激!谢谢!
解组后,方法的输入有效负载
SomeResponseTransformer.handle()
将不再是 aResponseEntity
,而是.expectedResponseType(Response.class)
。我不确定是什么
CONTEXTPATH_RESPONSE
,但我通常更喜欢使用setClassesToBeBound()
。另外,Jaxb2Marshaller
默认情况下,我们期望我们@XmlRootElement
绑定该类。然后,当响应从服务器到达时,您需要确保
Content-Type
标头是受该标头支持的MarshallingHttpMessageConverter
:RestTemplate
从这里开始调试您的调用并查看其doExecute()
aroundrequestCallback.doWithRequest(request);
和中的作用可能会更好responseExtractor.extractData(response)
。