1.如果controlloer上用@ResponseBody注解,则用<mvc:message-converter>里面配置的json解析器进行解析
1 2 3 4 5 6 7 8 9 10 11 12 13 | <mvc:annotation-driven> <mvc:message-converters register-defaults= "true" > <bean class = "com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" > <property name= "supportedMediaTypes" value= "text/html;charset=UTF-8" /> <property name= "features" > <array> <value>WriteMapNullValue</value> <value>WriteNullStringAsEmpty</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> |
2.如果希望自定义View进行解析,则不能在controller上加 @ResponseBody注解,理由看一.
自定义View进行json和jsonp格式的自动判断并返回相应的json格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class JsonView implements View { private static final String CONTENT_TYPE = "application/json" ; private static final String RESULT = "result" ; @Override public String getContentType() { return CONTENT_TYPE; } @Override public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { if (model != null && model.containsKey(RESULT)){ Result result = (Result)model.get(RESULT); if (result != null ){ ResultUtils.render(result, request, response); } } } } |
配置ContentNegotiatingViewResolver,进行json解析器选择
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <bean id= "contentNegotiationManager" class = "org.springframework.web.accept.ContentNegotiationManagerFactoryBean" > <property name= "favorPathExtension" value= "true" /> <property name= "favorParameter" value= "true" /> <property name= "ignoreAcceptHeader" value= "true" /> <property name= "ignoreUnknownPathExtensions" value= "false" /> <property name= "mediaTypes" > <map> <entry key= "json" value= "application/json" ></entry> <entry key= "xml" value= "application/xml" ></entry> <entry key= "html" value= "text/html" ></entry> </map> </property> </bean> <bean id= "contentNegotiatingViewResolver" class = "org.springframework.web.servlet.view.ContentNegotiatingViewResolver" > <property name= "order" value= "0" /> <property name= "contentNegotiationManager" ref= "contentNegotiationManager" /> <property name= "useNotAcceptableStatusCode" value= "false" /> <!-- 设置默认视图 --> <property name= "defaultViews" > <list> <bean class = "com.tools.util.JsonView" ></bean> <bean class = "com.tools.util.JsonpView" ></bean> </list> </property> <!-- 设置视图解析器 --> <property name= "viewResolvers" > <list> <ref bean= "viewResolver" /> </list> </property> </bean> |
order:如果存在多个viewResolver则order值小的被使用,如果没有合适的viewResolver则会使用另外的;
favorPathExtension:是否支持扩展名,默认为true(支持),扩展名指的xxx.json、xxx.xml等形式
favorParameter:是否启用参数支持,默认为true(支持),即xxx?format=json、xxx?format=xml等形式,这里的参数名默认为format,可以通过配置改变。
ignoreAcceptHeader:是否忽略accept header,默认是false(不忽略),即请求时指定的contentType:application/json等,由于我这里要使用扩展名的形式返回,所以把另外两项都关闭了,可视不同情况,使用不同设置;
mediaTypes:配置扩展名到mimeType的映射,这里配置了json和xml的映射;
defaultViews:配置视图,这里配置了json和xml的视图,json使用的jackson;
这样就可以实现不同的文件扩展名用不同的View解析器进行解析
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地