1. 程式人生 > >SpringMVC返回字串去掉引號

SpringMVC返回字串去掉引號

在springMVC中使用@ResponseBody註解時會強制返回json格式,在返回字串時會預設加上雙引號。去掉引號可以在springMVC配置檔案中加入如下配置項:

<mvc:annotation-driven>
	<mvc:message-converters>
		<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
		<bean class="org.springframework.http.converter.StringHttpMessageConverter" /> 
	</mvc:message-converters>
</mvc:annotation-driven>
也可以在配置JSON返回模板時直接配置進去,配置方法如下:
<!-- 返回JSON模版 -->
	<bean id="mappingJackson2HttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/json;charset=UTF-8</value>
				<!-- <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> -->
			</list>
		</property>
	</bean>
	<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" />
	
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJackson2HttpMessageConverter" />
				<ref bean="stringHttpMessageConverter" />
			</list>
		</property>
	</bean>