我试图用Spring Rest发送文件列表,但我得到此异常。春季休息 - 发送文件列表时发生异常
Could not write content: No serializer found for class java.io.ByteArrayInputStream
它与一个文件(ByteArrayResource
)。
它不适用于文件列表(List<ByteArrayResource>
)。
这里是我的代码的重要部分:
List<ByteArrayResource> contentsAsResource = new ArrayList<ByteArrayResource>();
for(MultipartFile fichier : fichiers) {
contentsAsResource.add(new ByteArrayResource(fichier.getBytes()) {
@Override
public String getFilename()
{
return fichier.getOriginalFilename();
}
});
};
map.add("files", contentsAsResource);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(map, headers);
FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
formConverter.setMultipartCharset(Charset.forName("UTF8"));
formConverter.addPartConverter(new MappingJackson2HttpMessageConverter());
this.restClientHelper.getRestTemplate().getMessageConverters().add(formConverter);
this.restClientHelper.getRestTemplate().postForObject("file/save", requestEntity, Object.class);
我试过,没有工作的情况如下:
- 发送一个数组,而不是一个列表
- 包装清单在DTO
- 发送字节[],但它并没有在文件工作>有1.5Mb
我一直在调试反序列化,但理解起来非常痛苦!
如果它可以帮助,使用一个文件,转换器'ResourceHttpMessageConverter
'被使用。
任何人有想法吗?
编辑:如果我通过文件在地图(而不是列表)中添加每个文件,请求的作品。
for (MultipartFile fichier : fichiers) {
map.add("files", new ByteArrayResource(fichier.getBytes()) {
@Override
public String getFilename()
{
return fichier.getOriginalFilename();
}
});
};
但我得到另一个例外:org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/myApp/ws/file/save"
。在目标服务器上启用了基本身份验证。如果我禁用它,一切正常!以下是目标服务器上的spring-security配置。
<http pattern="/ws/file/**" authentication-manager-ref="basicAuthenticationManager" create-session="stateless">
<intercept-url pattern="/**" access="isAuthenticated()"/>
<http-basic />
<csrf disabled="true" />
<headers disabled="true" />
</http>
我在弹簧安全配置中丢失了什么吗?
编辑2:好像没有在报头中的令牌“授权”,将它添加手动解决问题
编辑3:问题解决了!当使用基本身份验证启用时,将具有弹簧休息模板的多个文件发送到目标服务器时,需要(重新)添加基本身份验证令牌。这里有很好的解释:Basic authentication for REST API using spring restTemplate。我不知道它是否是一个错误(从Spring开始)。此更新之前,我的配置(我选择的拦截方法)是这样的:
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(login, motDePasse));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
...
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClientBuilder.build());
this.restTemplate = new RestTemplate(requestFactory);
我已经添加了这一点:
this.restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));
类是:
private static class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor
{
private final String username;
private final String password;
public BasicAuthorizationInterceptor(String username, String password)
{
this.username = username;
this.password = (password == null ? "" : password);
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException
{
byte[] token = Base64.getEncoder().encode((this.username + ":" + this.password).getBytes());
request.getHeaders().add("Authorization", "Basic " + new String(token));
return execution.execute(request, body);
}
}
[请参阅本文](http:// stackoverflow。com/questions/25103070/resttemplate-httpmessagenotwritableexception-no-serializer-found-for-class-ja) – Onkar
谢谢。我看过这篇文章。我的代码非常相似。 – Clemzd
所以你的问题不再是关于春天的休息模板,而是春天的安全?如果你想支持目标服务器上的安全性,你的请求应该被认证(设置头文件例如:'Authorization:Basic QWxhZGRpbjpPcGVuU2VzYW1l' –