Fastjson1.2.7 / 1.2.15 /1.2.16 与spirngmvc4.2+兼容问题
这里有个wiki建议配置。
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
<!-- MediaTypes -->
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
<!-- FastJsonConfig -->
<property name="fastJsonConfig" ref="fastJsonConfig" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
然后问题就来了。无论我用
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4
亦或是
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
这两个类,在springmvc4.2.5 4.1.6中 一旦配置了
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
配置了这一段,springmvc 在 @ResponseBody 后 会返回406.也就是 网上都会搜到的 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 我一旦把
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
去掉。变成
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
变成这样以后,一切正常,不论是4.2.5 还是4.1.6 闹心。 我追看了下源码
public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConverter<T> {
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private List<MediaType> supportedMediaTypes = Collections.emptyList();
/**
* Set the list of {@link MediaType} objects supported by this converter.
*/
public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
Assert.notEmpty(supportedMediaTypes, "'supportedMediaTypes' must not be empty");
this.supportedMediaTypes = new ArrayList<MediaType>(supportedMediaTypes);
}
的确有set注值的地方。不解。求关爱帮助
下面贴下jar包环境
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>
<spring.version>4.2.5.RELEASE</spring.version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
SerializerFeature 对于这个 枚举,能否把中文解释贴出。我没地方找啊
@wenshao
@yl23250 #671
@VictorZeng 道哥,我感觉我和#671不全一样啊。我这里是返回 status = 406 。再一个是。我是跟着wiki做下来。
<property` name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
这段代码有问题引起的。而且我jquery ajax的dataType也一直有“json”。 我从1.1*的版本开始用了。我一直记得supportedMediaTypes这个是返回给前台用的文本类型啊 我要怎么改呢。 再者,就是跟着wiki上的spring4.2+配置有问题。。希望更新下
@yl23250 supportedMediaTypes的含义是支持的数据类型,包括接收和返回,如果不设置相当于所有类型都支持,如果设置supportedMediaTypes 为 application/json相当于仅仅支持Content-Type:application/json这一种数据类型,你的问题应该是Content-Type并没有设置,默认的application/x-www-form-urlencoded ,所以406,Fastjson1.2.7的时候对这个并没有严格的控制。