在项目的文件管理模块中有用到自定义content-type,以及自动匹配HttpMessageConverter,
某童鞋 在解决这些问题的时候遇到了点困难
今天研究了半天分享下 springmvc通过自定义http accept或者content-type自动选择HttpMessageConverter
RestTmplate设置headers:
restTemplate设置headers有三种方式
1、通过设置MessageConverters
2、通过设置拦截器(ClientHttpRequestInterceptor)
3、通过配置HttpHeaders
一般的restTemplate请求都可以设置HttpHeaders,因此HttpHeaders是比较容易想到的。
但使用http get方式的时候restTemplate没有设置HttpHeaders的地方
文件管理器模块有个功能是直接获取文件的字节数组返回给客户端,以提高javascript客户端的解析速度
返回byte[] 需要有相应解析器:ByteArrayHttpMessageConverter。它对应的Content-Type是application/octet-stream
而我们用的是自定义的:application/bytes ,
在服务端配置文件加入下面配置
<bean id="byteConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/bytes</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter ">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
//信息转换器放入适配器中
<ref bean="byteConverter" />
</list>
</property>
</bean>
然后在方法上设置headers
@RequestMapping(value = "/public/{appId}/{fileName}", method = RequestMethod.GET, headers = { "content-type=application/bytes" })
服务端的配置完成
客户端restTemplate不能使用HttpHeaders,只能用后两种
RestTemplate初始化的时候会加入5个信息转换器,其中就有ByteArrayHttpMessageConverter,
这样到服务端加上自己设置的就有6种类型,在自动匹配HttpMessageConverter会匹配到不是自己想要的转换器,比如这6个在getBytes这个功能的测试中,会匹配成json的转换器。
为避免这样就在RestTemplate请求前,用自定义Content-Type的ByteArrayHttpMessageConverter覆盖默认的配置
通过设置HttpMessageConverters
ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.valueOf("application/bytes")));
restTemplate.setMessageConverters((List)Collections.singletonList(converter));
通过设置ClientHttpRequestInterceptor
ClientHttpRequestInterceptor interceptor= new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
MediaType mt=MediaType.valueOf("application/bytes");
wrapper.getHeaders().setContentType(mt);
wrapper.getHeaders().setAccept((List)Collections.singletonList(mt));
return execution.execute(wrapper, body);
}
};
restTemplate.setInterceptors(Collections.singletonList(interceptor));
分享到:
相关推荐
5. ** MediaType**:在处理请求和响应时,SpringMVC会检查请求头中的`Accept`字段和响应头中的`Content-Type`字段,以确定应该使用哪种类型的转换器。对于JSON,其媒体类型是`application/json`。 6. **HTTP GET与...
1. **Content-Type设置不正确**:在控制器方法中,你需要确保返回的`@ResponseBody`或`@RestController`注解的响应具有正确的`Content-Type`,通常是`application/json`。例如: ```java @RequestMapping(value = ...
Spring MVC的消息转换器(MessageConverter)是框架的核心组件之一,它们负责在HTTP请求和响应之间进行数据转换。...在实际开发中,可以根据需求自定义消息转换器或者扩展已有的转换器,以满足特定的数据交换需求。
3. **检查Content-Type**:确保在Controller方法上设置了正确的`produces`属性,指定返回的Content-Type为`application/json`。例如: ```java @RequestMapping(value = "/api", method = RequestMethod.GET, ...
.defaultContentType(MediaType.APPLICATION_JSON) .mediaType("xml", MediaType.APPLICATION_XML) .mediaType("html", MediaType.TEXT_HTML) .mediaType("json", MediaType.APPLICATION_JSON); } ``` 接下来,...