这是一个spring集成(基于spring boot)项目,我配置了spring集成应用程序如下:,
配置xml:
<int:channel id="httpChannel"/>
<int-http:inbound-gateway supported-methods="GET,POST" request-channel="httpChannel" reply-channel="outputChannel" path="/"
message-converters="converter"
/>
<bean id="converter" class="in.tc.springintegrationdemo.CustomConverter" />
<int:transformer ref="customTransformer" input-channel="httpChannel" output-channel="outputChannel" />
<bean id="customTransformer" class="in.tc.springintegrationdemo.MultipartTransformer" />
<bean class="in.tc.springintegrationdemo.MessageUpdateBean" id="printMessage"/>
<int:service-activator ref="printMessage" method="print" input-channel="outputChannel">
</int:service-activator>
自定义转换器定义如下
public class CustomConverter implements HttpMessageConverter<MultiValueMap<String,?>> {
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
...
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}
@Override
public List<MediaType> getSupportedMediaTypes() {
return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
}
@Override
public MultiValueMap<String,?> read(Class<? extends MultiValueMap<String,?>> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
MultiValueMap<String, ?> result = new LinkedMultiValueMap<>();
String str = "";
MediaType contentType = inputMessage.getHeaders().getContentType();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputMessage.getBody(), contentType.getCharset()));
while((str=bufferedReader.readLine())!=null) {
//code to retrieve multipart data
}
return result;
}
@Override
public void write(MultiValueMap<String,?> t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
throw new UnsupportedOperationException();
}
对于多部分/表单数据,我总是在customconverter类中得到一个空的负载。
我还尝试添加一个多部分解析器,如下所示
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> but it has the same result.
我在stackoverflow-spring集成dsl-http入站网关使用多部分/表单数据时也遇到了类似的问题。但对我来说没用。
多部分/表单数据负载
POST / HTTP/1.1
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Postman-Token: d24d89ef-c52b-492a-96b9-6ef6807e8d1a
Host: localhost:9090
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------865732735860935509694157
Content-Length: 217
----------------------------865732735860935509694157
Content-Disposition: form-data; name=""; filename="testfile.txt"
Content-Type: text/plain
testcontent
----------------------------865732735860935509694157--
谢谢期待。
1条答案
按热度按时间wbgh16ku1#
我想你应该放弃你的计划
CustomConverter
支持现有的现成的-MultipartAwareFormHttpMessageConverter
,在任何<int-http:inbound-gateway>
bean定义。既然你说你用的是Spring Boot,那么你就不需要用Spring Boot做任何事了
multipartResolver
-它是为我们自动配置的开箱即用MultipartAutoConfiguration
.如果这仍然不适用于你,我很高兴看到一个简单和简短的版本,你的项目玩,复制和可能的修复。
不相关:有没有机会理解为什么要使用xml配置,而不是java&annotations甚至javadsl?