spring mvc返回json串解决手机应用网络请求

转载:http://www.apk-doc.net/spring-mvc/spring-mvc-json.html

做app应用开发时,手机客户端适合处理json串,而在java开发中,最方便的快速简洁的服务端开发技术莫过于使用spring mvc注解方式构建web应用返回json串

第一步:

搭建spring mvc环境:

工程依赖jar

spring mvc返回json串解决手机应用网络请求spring mvc返回json串解决手机应用网络请求

<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>
 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
spring mvc配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 
 
http://www.springframework.org/schema/mvc
 
 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
 
 
http://www.springframework.org/schema/context
 
              http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
	<context:component-scan base-package="com.smsc"/>
 
 
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/views/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
 
 
 
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
 
		<property name="messageConverters">
			<list>
				<ref bean="stringHttpMessageConverter" />
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
 
	</bean>
	<bean id="stringHttpMessageConverter"
		class="org.springframework.http.converter.StringHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/plain;charset=UTF-8</value>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
 
	</bean>
 
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
 
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=UTF-8</value>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
 
	</bean>
	<mvc:annotation-driven
		content-negotiation-manager="contentNegotiationManager" />
	<bean id="contentNegotiationManager"
		class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
		<property name="favorPathExtension" value="false" />
		<property name="favorParameter" value="false" />
		<property name="ignoreAcceptHeader" value="false" />
		<property name="mediaTypes">
			<value>
				atom=application/atom+xml
				html=text/html
				json=application/json
				*=*/*  
        </value>
		</property>
	</bean>
</beans>

 测试访问,默认导向index.jsp,工程搭建成功

spring mvc返回json串解决手机应用网络请求