NutzCN Logo
问答 spring mvc fastjson 转换了两次 如何解决呢
发布于 122天前 作者 Hamming 355 次浏览 复制 上一个帖子 下一个帖子
标签:

fastjson 转换了两次 如何解决呢
登录成功返回的是string类型的json {"code":0,"count":0,"msg":"login.success"}
二不是 json
spring mvc 集成 fastjson
之前 nutzboot 也遇到类似的问题 请教 如何解决呢
求大神解答


<!-- 启用默认配置 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> <value>*/*;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> @Configuration public class FastJSONMessageConverter extends ConfigurableWebBindingInitializer { @Bean public HandlerAdapter handlerAdapter(WebBindingInitializer webBindingInitializer) { RequestMappingHandlerAdapter requestMappingHandlerAdapter = new RequestMappingHandlerAdapter(); //参数及返回值处理 List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); //注入FastJSON FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // log.info("converters:" + converters.toString()); List<MediaType> supportMediaTypeList = new ArrayList<>(); supportMediaTypeList.add(MediaType.TEXT_HTML); supportMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8); supportMediaTypeList.add(MediaType.IMAGE_GIF); supportMediaTypeList.add(MediaType.IMAGE_JPEG); supportMediaTypeList.add(MediaType.IMAGE_PNG); FastJsonConfig config = new FastJsonConfig(); SerializerFeature[] serializerFeatures = new SerializerFeature[]{ // 输出key是包含双引号 // SerializerFeature.QuoteFieldNames, // 是否输出为null的字段,若为null 则显示该字段 SerializerFeature.WriteMapNullValue, // 数值字段如果为null,则输出为0 SerializerFeature.WriteNullNumberAsZero, // List字段如果为null,输出为[],而非null SerializerFeature.WriteNullListAsEmpty, // 字符类型字段如果为null,输出为"",而非null SerializerFeature.WriteNullStringAsEmpty, // Boolean字段如果为null,输出为false,而非null SerializerFeature.WriteNullBooleanAsFalse, // Date的日期转换器 SerializerFeature.WriteDateUseDateFormat, // 循环引用 // SerializerFeature.DisableCircularReferenceDetect, }; config.setDateFormat("yyyy-MM-dd HH:mm:ss"); config.setSerializerFeatures(serializerFeatures); converter.setFastJsonConfig(config); converter.setSupportedMediaTypes(supportMediaTypeList); converter.setDefaultCharset(Charset.forName("UTF-8")); converters.add(converter); //参数及返回值处理 requestMappingHandlerAdapter.setMessageConverters(converters); //参数校验 requestMappingHandlerAdapter.setWebBindingInitializer(webBindingInitializer); return requestMappingHandlerAdapter; } }
3 回复

哥们,跑错片场啦、、、

想与您洽谈合作TG:tengfei_sir

添加回复
请先登陆
回到顶部