使用Resttemplate : HttpMessageNotWritableException: No serializer found for class java.io.ByteArrayInputStream中描述的相同设置我无法从后端API获得响应,目标是通过HTTP上传文件并将其存储在MongoDB中,需要时通过使用某些http标头进行GET调用(可以正常工作)。
服务激活器内部的方法返回相同的请求消息...将文档中的返回类型更改为有效内容中包含文档的消息,但它不会生成不同的行为,基本上文件保存在MongoDB(http调用工作),但没有响应发送到前端。
另一方面,使用纯XML配置,我不确定如何从UploadedMultiPartFile中提取文件并将其转发到后端API(请参阅下面的其他设置)。
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!
}
请求/响应标头
Remote Address:::1:8080
Request URL:http://localhost:8080/gateway
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ro;q=0.6,nb;q=0.4
Connection:keep-alive
Content-Length:55705
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary93sxUpjZ6rxmxsGc
Cookie:m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D
DNT:1
Host:localhost:8080
Origin:chrome-extension://cokgbflfommojglbmbpenpphppikmonn
service:file-ul
urlpath:upload
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Request Payload
------WebKitFormBoundary93sxUpjZ6rxmxsGc
Content-Disposition: form-data; name="file"; filename="a3YxYwN_460s.jpg"
Content-Type: image/jpeg
------WebKitFormBoundary93sxUpjZ6rxmxsGc--
Response Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ro;q=0.6,nb;q=0.4
Content-Length:55705
Content-Type:multipart/form-data;boundary=----WebKitFormBoundary93sxUpjZ6rxmxsGc
Cookie:m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D
DNT:1
Host:localhost:8080
http_requestMethod:POST
http_requestUrl:http://localhost:8080/gateway
Origin:chrome-extension://cokgbflfommojglbmbpenpphppikmonn
Server:Jetty(8.1.15.v20140411)
service:file-ul
urlpath:upload
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
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>
答案 0 :(得分:0)
我决定最终通过一个单独的URL处理多部分文件上传,在Spring MVC中创建一个虚假端点,并将Multipart文件发布到该URL并将该请求转发到后端系统,如果可以的话,它会很好通过http出站网关处理multipartfileupload ...但事实并非如此。