REST
REST(英文:Representational State Transfer)。REST描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中。使用的是标准的 HTTP 方法,比如 GET、PUT、POST和DELETE.REST 简化了客户端和服务器的实现.表现层
REST的名称"表现层状态转化"。"表现层"其实指的是"资源"(Resources)的"表现层"。所谓"资源",就是网络上的一个实体,或者说是网络上的一个具体信息。可以是任意mime type。另外,URI只代表资源的实体,不代表它的形式,确切的说,URI应该只代表"资源"的位置。它的具体表现形式,应该在HTTP请求的头信息中用Accept和Content-Type字段指定,这两个字段才是对"表现层"的描述。下面通过一个Web例子说明>首先要准备REST所需的jar包,这里交给maven。<span style="white-space:pre"> </span><dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.0</version>
</dependency>
在spring的配置文件中配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true">
<!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
<!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
<mvc:resources location="/*.html" mapping="/**.html" />
<mvc:resources location="/css/*" mapping="/css/**" />
<mvc:resources location="/js/*" mapping="/js/**" />
<mvc:resources location="/images/*" mapping="/images/**" />
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="com.my.web.*" />
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" /><!-- application/xml -->
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean id="jsonView"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean id="xmlView"
class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.my.web.bean.Employee</value>
<!-- <value>com.cnblogs.yjmyzz.dto.ListBean</value> -->
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
</beans>
注意,defaultContentType 是默认的资源表现形式,常见有“application/json", "application/xml" , "test/html"。
mediatypes用于设置http request请求的数据表现形式转换关系。
为请求编写controller
/**
*
*/
package com.my.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.my.web.bean.Employee;
import com.my.web.bean.ListBean;
import com.my.web.service.EmployeeService;
@Controller
@RequestMapping(value = "/rest", method = RequestMethod.GET)
public class MyController {
@Autowired
EmployeeService service;
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public Employee show(@PathVariable int id, HttpServletRequest request,
HttpServletResponse response) throws Exception {
return service.getEmployee(id);
}
}
构造所需数据
package com.my.web.service.impl;
import java.math.BigDecimal;
import org.springframework.stereotype.Service;
import com.my.web.bean.Employee;
import com.my.web.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Override
public Employee getEmployee(int id) {
return new Employee("hust", new BigDecimal("50"), false, 5);
}
}
构建Bean
package com.my.web.bean;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
private BigDecimal salary;
private boolean isVip;
private int id;
public Employee(String userName, BigDecimal salary,
boolean isVip, int id) {
this.userName = userName;
this.salary = salary;
this.isVip = isVip;
this.id = id;
}
public Employee() {
}
@XmlElement
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@XmlElement
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public boolean isVip() {
return isVip;
}
public void setVip(boolean isVip) {
this.isVip = isVip;
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return "id:" + this.id + ",userName:" + this.userName + ",isVip="
+ this.isVip;
}
}
注意:
1. 对元素属性加
@XmlElement
2. 一定要添加默认构造函数
3. 覆写toString()方法(我的是不写会报错)。
为了URI能被rest处理在web.xml修改pattern如下
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认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>
启动server,我们就可以通过URI获取我们的资源了。 比如http://localhost:8080/restful/rest/user/5.xml。其中.xml我们指定返回资源的表现形式。