Spring MVC REST 配置详解

从Spring 3.0 开始,spring提供了对REST的强大支持。Spring MVC作为一个MVC框架,有简单易用的特点。首先看一下我的配置文件。 web.xml文件的配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringRest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>SpringMVCServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>SpringMVCServlet</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>SpringMVCServlet</servlet-name>
  	<url-pattern>*.xml</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>SpringMVCServlet</servlet-name>
  	<url-pattern>*.json</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
  	<servlet-name>SpringMVCServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
 

这样配置的目的是,让Spring MVC 的DispatchServlet能够拦截所有的请求,但是根据Servlet URL pattern的匹配规则,如果我们在这里匹配.jsp文件的时候可能出错,所以建议将jsp文件的解析使用容器进行,不要使用viewResolver进行了。.xml,.json,.do三种配置使Spring DispatchServlet能够捕获三种不同文件方式的请求。

下面看一下,SpringMVCServlet文件的配置。

<?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"
	    xsi:schemaLocation="http://www.springframework.org/schema/beans
	        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	        http://www.springframework.org/schema/context
	        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	 
	<context:component-scan base-package="controller.web" />
	  
   	<bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
   		<property name="order" value="1"></property>
   		<property name="ignoreAcceptHeader" value="true"/>  
 		<property name="mediaTypes">  
     		<map>  
         		<entry key="json" value="application/json" />  
        		<entry key="xml" value="application/xml" />
        		<entry key="html" value="text/html" />
     		</map>  
 		</property>
 		
 		<property name="defaultViews">  
    		<list>  
         		<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />   
         		<bean class="org.springframework.web.servlet.view.xml.MarshallingView" >  
             		<constructor-arg>
             			<bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
             		</constructor-arg>  
         		</bean>  
     		</list>  
 		</property>
   	</bean>
   	
   	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   		<property name="order" value="2"></property>
		<property name="prefix" value="/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

 根据REST原则中,资源与资源的显示方式相隔离的原则,Spring REST中引入了Content Negotiation技术,对数据的显示方式进行定制。由于我们的显示方式规定为XML,Json和JSP的样式,所以我们在Content Negotiation中配置了XML和JSON的viewResolver。最后一个viewResolver是一个默认的resolver当Content Negotiation无法解析的时候将使用这个viewResolver。

viewResolver的使用顺序。在我们代码中配置了order属性来管理,viewResolver的使用顺序。

UserController代码:

package controller.web;

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

@Controller
public class HelloWorldController {

	@RequestMapping("/hello")
	public ModelAndView helloWorld(){
		
		String message = "Hello World, Spring 3.0";
		return new ModelAndView("hello","message",message);
	}
}

 @Controller注解表示该类为一个Controller类。

@RequestMapping("/hello")注解,表示Spring Dispatch Servlet捕获到的/hello的请求都由该方法进行处理。

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="hello.jsp">Say Hello</a>
</body>
</html>

 /webcontent/pages/hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}asdfasdf
</body>
</html>
 

好了配置结束我们可以测试一下:

http://localhost:8080/SpringRest/

然后点击SayHello,结果会出错 :message /SpringRest/hello.jsp

因为.jsp类型的文件的解析是由Tomcat容器进行的,但是我们的jsp文件位于/pages/hello.jsp,所以会出错。

如果我们使用下面的url路径:http://localhost:8080/SpringRest/pages/hello.jsp就能够正常访问这个文件了。

但是Spring MVC的设计者们不建议我们这样做,要求我们将.jsp文件放在/web-inf目录下,防止用户直接访问。

我们讲index.jsp中的超级链接修改为hello.do就可以正常访问了。

我们可以通过http://localhost:8080/SpringRest/hello.xml。http://localhost:8080/SpringRest/hello.json。http://localhost:8080/SpringRest/hello.html等各种方式来获得我们需要的资源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值