tl;博士
我有一个使用 XML 正文进行响应的 REST 服务。我想使用 HTTP 消息转换器来解码响应。但是,从服务返回的 XML 并不总是映射到单个 Java 类型。如何配置 HTTP 出站网关以期望多种响应类型?
<小时/>我正在使用
- Java 1.6
- Spring 集成 3.0.0.RELEASE
- Spring 集成 HTTP 3.0.0.RELEASE
- Spring OXM 4.0.1.RELEASE
我正在尝试设置一个调用 REST 服务的 int-http:outbound-gateway
。 REST 服务以“text/xml”响应。我希望能够使用 MarshallingHttpMessageConverter将响应编码到 Java 对象。
REST 服务可以使用“成功”XML 响应或“错误”XML 响应进行响应。例如,它可能返回:
<?xml version="1.0" encoding="UTF-8"?>
<success>
<yay>You're call was successful, here is the information you wanted.</yay>
<information>very important stuff</information>
</success>
或者它可能会返回:
<?xml version="1.0" encoding="UTF-8"?>
<failure>
<boo>You're call was unsuccessful, go in the corner and cry.</boo>
<error>very important error code</error>
</failure>
因此,我需要设置 2 个 Java 对象来映射这些不同的响应。所以我创建了
成功.java:
@XmlRootElement(name = "success")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Success{
private String yay;
private String information;
@XmlElement(name = "yay")
public String getYay() {
return yay;
}
@XmlElement(name = "information")
public String getInformation() {
return information;
}
//setters omitted for brevity.
}
失败.java:
@XmlRootElement(name = "failure")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Failure{
private String boo;
private String information;
@XmlElement(name = "boo")
public String getBoo() {
return boo;
}
@XmlElement(name = "error")
public String getError() {
return error;
}
//setters omitted for brevity.
}
现在,为了设置出站网关,我有一个使用 MarshallingHttpMessageConverter
配置的 RestTemplate
。在 MarshallingHttpMessageConverter
上,我注入(inject)了一个已绑定(bind) Success 和 Failure 对象的 OXM 解码器:
<oxm:jaxb2-marshaller id="myRestMarshaller">
<oxm:class-to-be-bound name="com.example.Success" />
<oxm:class-to-be-bound name="com.example.Failure" />
</oxm:jaxb2-marshaller>
问题是,当我尝试设置 int-http:outbound-gateway
时,我只能放置 com.example.Success
或 之一>com.example.Failure
到 expected-response-type
属性中。如果我选择使用 Success
,如果 REST 服务响应 Failure
,它将引发异常,反之亦然。
<int-http:outbound-gateway
id="myRestServiceGateway"
url-expression="'http://localhost:8855/webapp/ws/testId/{testId}'"
http-method="GET"
request-channel="restRequest"
reply-channel="restResponse"
rest-template="restTemplate"
expected-response-type="com.example.Success">
<int-http:uri-variable name="testId" expression="payload"/>
</int-http:outbound-gateway>
如何告诉出站网关响应类型可以是 com.example.Success
或 com.example.Failure
?
最佳答案
Artem Bilan's answer按要求回答我的问题。然而,我最终使用了另一种方法来实现我的目标。
我设置了出站网关来接收字符串形式的 xml,但我没有使用 MarshallingHttpMessageConverter
。相反,我将出站网关放置在一个链中,其中网关是第一个端点,解码变压器是第二个端点。所以我的配置如下:
<oxm:jaxb2-marshaller id="myRestMarshaller">
<oxm:class-to-be-bound name="com.example.Success" />
<oxm:class-to-be-bound name="com.example.Failure" />
</oxm:jaxb2-marshaller>
<int:chain input-channel="restRequest" output-channel="restResponse">
<int-http:outbound-gateway
id="myRestServiceGateway"
url-expression="'http://localhost:8855/webapp/ws/testId/{testId}'"
http-method="GET"
rest-template="restTemplate"
expected-response-type="java.lang.String">
<int-http:uri-variable name="testId" expression="payload"/>
</int-http:outbound-gateway>
<int-xml:unmarshalling-transformer unmarshaller="myRestMarshaller"/>
</int:chain>
我更喜欢这种方法,因为我认为它提供了更多的关注点分离。现在网关只关心与 HTTP 服务的通信,编码器关心翻译响应。
我也不需要向我的模型引入一个永远不会使用的额外类。
关于java - 是否可以指定多个预期响应类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520719/