肯定有一些我想念的东西,所以在这里寻求帮助。
我正在使用带有以下HTTP入站网关的Spring Integration构建一个简单的REST应用程序:
<!-- Gateway -->
<int-http:inbound-gateway id="fruitQuotePOSTGateway"
request-channel="fruitQuotePOSTRequests"
supported-methods="POST"
path="/api/v1/fruit-quote"
request-payload-type="java.lang.String"
reply-timeout="10000"
reply-channel="fruitQuotePOSTResponses"
error-channel="applicationErrors">
<int-http:request-mapping consumes="application/xml" produces="application/xml"/>
</int-http:inbound-gateway>
XML进入此网关后,将执行以下简单步骤:
首先,这是整个应用程序的XML配置(为简洁起见,省略了HTTP名称空间):
<!-- Gateway -->
<int-http:inbound-gateway id="fruitQuotePOSTGateway"
request-channel="fruitQuotePOSTRequests"
supported-methods="POST"
path="/api/v1/fruit-quote"
request-payload-type="java.lang.String"
reply-timeout="10000"
reply-channel="fruitQuotePOSTResponses"
error-channel="applicationErrors">
<int-http:request-mapping consumes="application/xml" produces="application/xml"/>
</int-http:inbound-gateway>
<!--
- Generate fruit quote request JAXB from the incoming request
- Create a header "requestUUID" by reading it from fruit quote request JAXB
- Generate fruit quote acknowledgement response for the calling client
-->
<int:transformer input-channel="fruitQuotePOSTRequests"
ref="fruitQuoteTransformation"
method="generateFruitQuoteRequestJAXB"/>
<int:header-enricher input-channel="requestUUIDEnrichment" output-channel="orderIDGeneration">
<int:header name="requestUUID" expression="payload.getFruitQuoteRequestJAXB().getFRUITQUOTEREQUESTDATA().getUuid()"/>
</int:header-enricher>
<int:transformer input-channel="fruitQuoteAcknowledgementGeneration"
ref="fruitQuoteTransformation"
method="generateFruitQuoteAcknowledgement"
output-channel="fruitQuotePOSTResponses"/>
<!-- Error handling -->
<int:transformer input-channel="applicationErrors"
ref="fruitQuoteTransformation"
method="generateFruitQuoteAcknowledgementWithError"
output-channel="fruitQuotePOSTResponses"/>
<!-- Channels -->
<int:channel id="fruitQuotePOSTRequests"/>
<int:channel id="requestUUIDEnrichment"/>
<int:channel id="fruitQuotePOSTResponses"/>
<int:channel id="fruitQuoteAcknowledgementGeneration"/>
<int:channel id="applicationErrors"/>
在应用程序中从一个步骤流到另一个步骤的有效负载是一个自定义的Builder对象,如下所示(省略了程序包名称):
import static java.util.Objects.nonNull;
public class FruiteQuoteComposite {
private final FRUITQUOTEREQUEST fruitQuoteRequestJAXB;
private final FruitQuoteApplicationException fruitQuoteApplicationException;
private final Integer orderID;
private final ErrorInformation errorInformation;
private FruiteQuoteComposite(FruiteQuoteCompositeBuilder fruiteQuoteCompositeBuilder) {
this.fruitQuoteRequestJAXB = fruiteQuoteCompositeBuilder.fruitQuoteRequestJAXB;
this.fruitQuoteApplicationException = fruiteQuoteCompositeBuilder.fruitQuoteApplicationException;
this.orderID = fruiteQuoteCompositeBuilder.orderID;
this.errorInformation = fruiteQuoteCompositeBuilder.errorInformation;
}
public FruitQuoteApplicationException getFruitQuoteApplicationException() {
return fruitQuoteApplicationException;
}
public FRUITQUOTEREQUEST getFruitQuoteRequestJAXB() {
return fruitQuoteRequestJAXB;
}
public Integer getOrderID() {
return orderID;
}
public ErrorInformation getErrorInformation() {
return errorInformation;
}
public static class FruiteQuoteCompositeBuilder {
private FRUITQUOTEREQUEST fruitQuoteRequestJAXB;
private FruitQuoteApplicationException fruitQuoteApplicationException;
private Integer orderID;
private ErrorInformation errorInformation;
public FruiteQuoteCompositeBuilder() {
}
public FruiteQuoteCompositeBuilder setFruitQuoteRequestJAXB(FRUITQUOTEREQUEST fruitQuoteRequestJAXB) {
if (nonNull(fruitQuoteRequestJAXB)) {
this.fruitQuoteRequestJAXB = fruitQuoteRequestJAXB;
}
return this;
}
public FruiteQuoteCompositeBuilder setFruitQuoteApplicationException(FruitQuoteApplicationException fruitQuoteApplicationException) {
if (nonNull(fruitQuoteApplicationException)) {
this.fruitQuoteApplicationException = fruitQuoteApplicationException;
}
return this;
}
public FruiteQuoteCompositeBuilder setOrderID(Integer orderID) {
if(nonNull(orderID)) {
this.orderID = orderID;
}
return this;
}
public FruiteQuoteCompositeBuilder setErrorInformation(ErrorInformation errorInformation) {
if (nonNull( errorInformation )) {
this.errorInformation = errorInformation;
}
return this;
}
public FruiteQuoteComposite build() {
return new FruiteQuoteComposite(this);
}
}
}
之所以没有在转换器上使用“输出通道”,是因为我想在运行转换的Java逻辑中明确选择replyChannel /传出路由。
例如,在 FruitQuoteTransformation.generateFruitQuoteRequestJAXB 方法中,我为成功设置了一条路径,为异常/错误设置了另一条路径,如下所示:
public Message<FruiteQuoteComposite> generateFruitQuoteRequestJAXB(Message<String> fruitQuoteRequestMessage) {
String fruitQuoteRequest = fruitQuoteRequestMessage.getPayload();
Unmarshaller unmarshaller;
FRUITQUOTEREQUEST fruitQuoteRequestJAXB;
try {
unmarshaller = requireNonNull(fruitQuoteRequestJaxbContext).createUnmarshaller();
fruitQuoteRequestJAXB = (FRUITQUOTEREQUEST) requireNonNull(unmarshaller)
.unmarshal(new StringReader(fruitQuoteRequest));
} catch (JAXBException jaxbException) {
logger.error("JAXB Unmarshalling exception occurred with error code :: " + ERR_FRUIT_QUOTE_REQUEST_JAXB_TRANSFORMATION, jaxbException);
FruitQuoteApplicationException fruitQuoteApplicationException = generateFruitQuoteApplicationException(ERR_FRUIT_QUOTE_REQUEST_JAXB_TRANSFORMATION, MESSAGE_FRUIT_QUOTE_INTERNAL_SYSTEM_ERROR);
FruiteQuoteComposite outboundFruitQuoteComposite = new FruiteQuoteComposite.FruiteQuoteCompositeBuilder()
.setFruitQuoteApplicationException(fruitQuoteApplicationException)
.build();
return withPayload(requireNonNull(outboundFruitQuoteComposite))
.setHeader(MessageHeaders.REPLY_CHANNEL, "applicationErrors")
.build();
}
FruiteQuoteComposite outboundFruitQuoteComposite = new FruiteQuoteComposite.FruiteQuoteCompositeBuilder()
.setFruitQuoteRequestJAXB(fruitQuoteRequestJAXB)
.build();
return withPayload(requireNonNull(outboundFruitQuoteComposite))
.setHeader(MessageHeaders.REPLY_CHANNEL, "requestUUIDEnrichment")
.build();
}
可以帮忙吗?
答案 0 :(得分:1)
您绝不能弄乱框架的replyChannel
标头;它不用于路由目的。
replyChannel
是一个内部通道,对于用于将回复与请求相关联的每条消息都是唯一的。通常,入站网关上不需要显式reply-channel
;如果这样做的话,它只是在运行时桥接到消息的replyChannel标头。
错误条件应该在网关的error-channel
上处理,而不是引发异常。不同的异常类型可以发出不同的错误信号。