使用Spring MVC搭建REST风格的服务

什么都不说了,直接上代码。

我使用Maven管理项目,JDK为1.7,Tomcat为7.0


Maven配置如下:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.2.6.RELEASE</version>
</dependency>

<!-- Servlet -->
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
</dependency>
<dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
</dependency>
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
</dependency>

<!-- for json -->
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.8.4</version>
</dependency>
<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.4</version>
</dependency>

<!-- for xml -->
<dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
</dependency>

web.xml配置如下:

<!-- Servlet that dispatches request to registered handlers (Controller 
	implementations). -->
	<servlet>
		<servlet-name>games</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/mvc-core-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>games</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
	<filter>
		<filter-name>httpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>httpMethodFilter</filter-name>
		<servlet-name>games</servlet-name>
	</filter-mapping>

mvc-core-config.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

        <!-- 启用spring mvc 注解,这个可以不配置。Spring在context:component-scan已经默认了 -->
        <!-- <context:annotation-config /> -->

        <!-- 扫描action包,应用Spring的注解 -->
        <context:component-scan base-package="com.tongbu.games.web" />

        <!-- 指定内容协商管理器的bean -->
        <mvc:annotation-driven
                content-negotiation-manager="contentNegotiationManager">
        </mvc:annotation-driven>

        <!-- 以下配置SpringMVC的拦截器 -->
        <mvc:interceptors>
                <bean id="requestResponseInterceptor"
                        class="com.tongbu.games.basic.iterceptor.RequestResponseInterceptor"></bean>
        </mvc:interceptors>

        <!-- 这样静态资源就可以访问了 -->
        <mvc:default-servlet-handler />

        <!-- Spring MVC的视图配置 -->
        <import resource="mvc-view-config.xml" />

        <!-- message处理见Spring例子 -->
        <bean id="messageSource"
                class="org.springframework.context.support.ResourceBundleMessageSource"
                p:basename="messages/messages" />

        <!-- 异常处理见Spring例子 -->
        <!-- Mapping exception to the handler view -->
        <bean id="exceptionResolver"
                class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
                <property name="exceptionMappings">
                        <props>
                        </props>
                </property>
        </bean>

</beans>

mvc-view-config.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 配置内容协商视图解析 -->
	<bean id="contentNegotiatingViewResolver"
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<!-- 设置内容协商管理器 -->
		<property name="contentNegotiationManager" ref="contentNegotiationManager"></property>
		<!-- 设置默认视图 -->
		<property name="defaultViews">
			<list>
				<ref bean="mappingJacksonJsonView" />
				<ref bean="marshallingView" />
			</list>
		</property>
		<!-- 设置视图解析器 -->
		<property name="viewResolvers">
			<list>
				<ref bean="defalutViewResolver" />
			</list>
		</property>
	</bean>

	<bean id="defalutViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- JSON视图 -->
	<bean id="mappingJacksonJsonView"
		class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"></bean>

	<!-- XML视图 -->
	<bean id="marshallingView"
		class="org.springframework.web.servlet.view.xml.MarshallingView">
		<property name="marshaller">
			<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
			</bean>
		</property>
	</bean>

	<bean id="contentNegotiationManager"
		class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
		<property name="mediaTypes">
			<props>
				<prop key="json">application/json;charset=utf-8</prop>
				<prop key="xml">application/xml;charset=utf-8</prop>
				<prop key="do">application/json;charset=utf-8</prop>
			</props>
		</property>
		<property name="ignoreAcceptHeader" value="true"></property>
		<!-- 设置默认的 ContentType为application/json-->
		<property name="defaultContentType" value="application/json"></property>
	</bean>

</beans>

Controller代码如下:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tongbu.games.web.BaseController;


@Controller
@RequestMapping("/testController")
public class TestController extends BaseController {
	
	@RequestMapping("/test")
	public ModelAndView test(){
		
		System.out.println(getRequest().getParameter("id"));
		ModelAndView mav = new ModelAndView();
		mav.addObject(getRequest().getParameter("id"));
		
		return mav;
	}
	
}








你可能感兴趣的:(spring,mvc,springMVC,REST)