spring 設定返回資料格式及json的fastjson配置
阿新 • • 發佈:2019-01-02
spring mvc配置檔案當中新增如下內容,實現responsebody返回String的編碼設定,
並且使用fastjson作為json解析器,自動解析java物件作為返回:
<!-- 預設的註解對映的支援,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping --> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"> <mvc:message-converters register-defaults="true"> <!-- @ResponseBody亂碼問題,將StringHttpMessageConverter的預設編碼設為UTF-8 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> <!-- 配置Fastjson支援 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="charset" value="UTF-8" /> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> <property name="features"> <list> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> <value>WriteDateUseDateFormat</value> <value>WriteEnumUsingToString</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- REST中根據URL字尾自動判定Content-Type及相應的View --> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="ignoreAcceptHeader" value="true" /> <property name="favorPathExtension" value="true" /> </bean>