Spring integration Spring集成ServiceActivator输出
使用与中所述相同的设置,我无法从后端API获得响应,目标是通过HTTP上传文件并将其存储在MongoDB中,需要时通过使用特定HTTP头的get调用检索文件 服务激活器中的方法返回相同的请求消息。。。将返回类型从文档更改为有效负载中包含文档的消息,但不会生成不同的行为,基本上,文件保存在MongoDB中http调用正常,但不会向前端发送响应 另一方面,使用纯XML配置,我不确定如何从UploadedMultiPartFile提取文件并将其转发到后端API。请参阅下面的另一个设置Spring integration Spring集成ServiceActivator输出,spring-integration,Spring Integration,使用与中所述相同的设置,我无法从后端API获得响应,目标是通过HTTP上传文件并将其存储在MongoDB中,需要时通过使用特定HTTP头的get调用检索文件 服务激活器中的方法返回相同的请求消息。。。将返回类型从文档更改为有效负载中包含文档的消息,但不会生成不同的行为,基本上,文件保存在MongoDB中http调用正常,但不会向前端发送响应 另一方面,使用纯XML配置,我不确定如何从UploadedMultiPartFile提取文件并将其转发到后端API。请参阅下面的另一个设置 public D
public Document uploadFile(Message inMessage) throws IOException {
LinkedMultiValueMap<String,Object> multipartRequest = (LinkedMultiValueMap<String, Object>) inMessage.getPayload();
final String filename = ((UploadedMultipartFile) multipartRequest.getFirst("file")).getOriginalFilename();
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("name", filename);
multipartMap.add("filename", filename);
byte[] bytes = ((UploadedMultipartFile) multipartRequest.getFirst("file")).getBytes();
ByteArrayResource contentsAsResource = new ByteArrayResource(bytes){
public String getFilename(){
return filename;
}
};
multipartMap.add("file", contentsAsResource);
Document result = template.postForObject("http://localhost:5050/api/upload", multipartMap, Document.class);
return result; // which isn't null!
}
XML设置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
<property name="inboundHeaderNames" value="*"/>
<property name="outboundHeaderNames" value="*"/>
<property name="userDefinedHeaderPrefix" value=""/>
</bean>
<int:channel id="http.frontend.rx"/>
<int:channel id="http.frontend.tx"/>
<int:channel id="http.backend.mysql"/>
<int:channel id="http.backend.mongo"/>
<int-http:inbound-gateway
id="frontEndToMySQLXX"
request-channel="http.frontend.rx"
reply-channel="http.frontend.tx"
header-mapper="headerMapper"
path="/gateway"
supported-methods="GET,POST,PUT,DELETE"/>
<int:router id="frontEndRouter" input-channel="http.frontend.rx" expression="headers.service">
<int:mapping value="json" channel="http.backend.mysql" />
<int:mapping value="file" channel="http.backend.mongo" />
</int:router>
<int-http:outbound-gateway
id="toMongoDB"
request-channel="http.backend.mongo"
reply-channel="http.frontend.tx"
url="http://localhost:5050/api/{path}"
http-method-expression="headers.http_requestMethod"
header-mapper="headerMapper"
expected-response-type="byte[]">
<int-http:uri-variable name="path" expression="headers['urlpath']"/>
</int-http:outbound-gateway>
<int-http:outbound-gateway
id="toMySQLDB"
request-channel="http.backend.mysql"
reply-channel="http.frontend.tx"
url="http://localhost:7070/api/{path}"
http-method-expression="headers.http_requestMethod"
expected-response-type="java.lang.String"
charset="UTF-8">
<int-http:uri-variable name="path" expression="headers['urlpath']"/>
</int-http:outbound-gateway>
我决定最终通过一个单独的URL处理多部分文件上传,在Spring MVC中创建一个伪端点,将多部分文件发布到该URL,并将该请求转发到后端系统,如果我可以通过http出站网关处理多部分文件上传,那就太好了。。。但事实并非如此
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
<property name="inboundHeaderNames" value="*"/>
<property name="outboundHeaderNames" value="*"/>
<property name="userDefinedHeaderPrefix" value=""/>
</bean>
<int:channel id="http.frontend.rx"/>
<int:channel id="http.frontend.tx"/>
<int:channel id="http.backend.mysql"/>
<int:channel id="http.backend.mongo"/>
<int-http:inbound-gateway
id="frontEndToMySQLXX"
request-channel="http.frontend.rx"
reply-channel="http.frontend.tx"
header-mapper="headerMapper"
path="/gateway"
supported-methods="GET,POST,PUT,DELETE"/>
<int:router id="frontEndRouter" input-channel="http.frontend.rx" expression="headers.service">
<int:mapping value="json" channel="http.backend.mysql" />
<int:mapping value="file" channel="http.backend.mongo" />
</int:router>
<int-http:outbound-gateway
id="toMongoDB"
request-channel="http.backend.mongo"
reply-channel="http.frontend.tx"
url="http://localhost:5050/api/{path}"
http-method-expression="headers.http_requestMethod"
header-mapper="headerMapper"
expected-response-type="byte[]">
<int-http:uri-variable name="path" expression="headers['urlpath']"/>
</int-http:outbound-gateway>
<int-http:outbound-gateway
id="toMySQLDB"
request-channel="http.backend.mysql"
reply-channel="http.frontend.tx"
url="http://localhost:7070/api/{path}"
http-method-expression="headers.http_requestMethod"
expected-response-type="java.lang.String"
charset="UTF-8">
<int-http:uri-variable name="path" expression="headers['urlpath']"/>
</int-http:outbound-gateway>