本文咱們將使用Spring MVC 4實現 CRUD Restful WebService , 經過RestTemplate寫一個 REST 客戶端,定義這些服務. 咱們也能夠經過外部的一些客戶端來測試這些服務。html
簡短 & 快速介紹REST
REST表示 Representational State Transfer(表示性狀態轉換).java
它是能夠用來設計web services的框架,能夠被不一樣的客戶端調用。web
核心思想是:使用簡單的HTTP協議來實現調用,而不是CORBA, RPC 或者 SOAP等負責的機制。spring
在Rest 基礎設計中,資源使用如下動詞進行操做。apache
- 建立資源 : 使用 HTTP POST
- 獲取資源 : 使用 HTTP GET
- 更新資源 : 使用 HTTP PUT
- 刪除資源 : 使用 HTTP DELETE
也意味着,你做爲Rest 服務開發者或者客戶,應該遵循以上的標準。json
儘管沒有限制必須返回的類型,可是通常基於Web services的Rest返回JSON或者XML做爲響應。api
客戶端能夠指定(使用HTTP Accept header)他們想要的資源類型嗎,服務器返回須要的資源。跨域
指明資源的Content-Type。若是想詳細的理解 restful能夠參考這裏:StackOverflow link 服務器
基於Rest的Controller(控制器)
咱們的 REST API :restful
- GET 方式請求 /api/user/ 返回用戶列表
- GET 方式請求 /api/user/1返回id爲1的用戶
- POST 方式請求 /api/user/ 經過user對象的JSON 參數建立新的user對象
- PUT 方式請求 /api/user/3 更新id爲3的發送json格式的用戶對象
- DELETE 方式請求/api/user/4刪除 ID爲 4的user對象
- DELETE 方式請求/api/user/刪除全部user
- package com.websystique.springmvc.controller;
-
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.util.UriComponentsBuilder;
-
- import com.websystique.springmvc.model.User;
- import com.websystique.springmvc.service.UserService;
-
- @RestController
- public class HelloWorldRestController {
-
- @Autowired
- UserService userService;
-
-
-
-
- @RequestMapping(value = "/user/", method = RequestMethod.GET)
- public ResponseEntity<List<User>> listAllUsers() {
- List<User> users = userService.findAllUsers();
- if(users.isEmpty()){
- return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
- }
- return new ResponseEntity<List<User>>(users, HttpStatus.OK);
- }
-
-
-
-
- @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<User> getUser(@PathVariable("id") long id) {
- System.out.println("Fetching User with id " + id);
- User user = userService.findById(id);
- if (user == null) {
- System.out.println("User with id " + id + " not found");
- return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
- }
- return new ResponseEntity<User>(user, HttpStatus.OK);
- }
-
-
-
-
-
- @RequestMapping(value = "/user/", method = RequestMethod.POST)
- public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
- System.out.println("Creating User " + user.getName());
-
- if (userService.isUserExist(user)) {
- System.out.println("A User with name " + user.getName() + " already exist");
- return new ResponseEntity<Void>(HttpStatus.CONFLICT);
- }
-
- userService.saveUser(user);
-
- HttpHeaders headers = new HttpHeaders();
- headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
- return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
- }
-
-
-
-
- @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
- public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user) {
- System.out.println("Updating User " + id);
-
- User currentUser = userService.findById(id);
-
- if (currentUser==null) {
- System.out.println("User with id " + id + " not found");
- return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
- }
-
- currentUser.setName(user.getName());
- currentUser.setAge(user.getAge());
- currentUser.setSalary(user.getSalary());
-
- userService.updateUser(currentUser);
- return new ResponseEntity<User>(currentUser, HttpStatus.OK);
- }
-
-
-
- @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
- public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
- System.out.println("Fetching & Deleting User with id " + id);
-
- User user = userService.findById(id);
- if (user == null) {
- System.out.println("Unable to delete. User with id " + id + " not found");
- return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
- }
-
- userService.deleteUserById(id);
- return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
- }
-
-
-
-
- @RequestMapping(value = "/user/", method = RequestMethod.DELETE)
- public ResponseEntity<User> deleteAllUsers() {
- System.out.println("Deleting All Users");
-
- userService.deleteAllUsers();
- return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
- }
-
- }
詳解:
@RestController :首先咱們使用的是Spring 4的新註解 @RestController註解.
此註解避免了每一個方法都要加上@ResponseBody註解。也就是說@RestController 本身戴上了 @ResponseBody註解,看以看做是
@Controller 和 @ResponseBody的結合體。
@RequestBody : 若是方法參數被 @RequestBody註解,Spring將綁定HTTP請求體到那個參數上。若是那樣作,Spring將根據請求中的ACCEPT或者 Content-Type header(私下)使用 HTTP Message converters 來將http請求體轉化爲domain對象。
@ResponseBody : 若是方法加上了@ResponseBody註解,Spring返回值到響應體。若是這樣作的話,Spring將根據請求中的 Content-Type header(私下)使用 HTTP Message converters 來將domain對象轉換爲響應體。
ResponseEntity 是一個真實數據.它表明了整個 HTTP 響應(response). 它的好處是你能夠控制任何對象放到它內部。
你能夠指定狀態碼、頭信息和響應體。它包含你想要構建HTTP Response 的信息。
@PathVariable 此註解意味着一個方法參數應該綁定到一個url模板變量[在'{}'裏的一個]中
通常來講你,要實現REST API in Spring 4 須要瞭解@RestController , @RequestBody, ResponseEntity 和 @PathVariable 這些註解 .另外, spring 也提供了一些支持類幫助你實現一些可定製化的東西。
MediaType : 帶着 @RequestMapping 註解,經過特殊的控制器方法你能夠額外指定,MediaType來生產或者消耗。
發佈和測試此API
http://localhost:8080/Spring4MVCCRUDRestService.
想要測試此API,我將使用POSTMAN這個外部客戶端,接下來咱們也將寫咱們本身的客戶端。
1. 獲取全部用戶
打開 POSTMAN工具,選擇請求類型爲GET,指明uri
注意:咱們沒有指明任何HTTP頭。點擊 發送,將接收到全部用戶的列表
也要注意HTTP 200 響應。
你也許好奇爲何此響應經過JSON字符串發送的,在響應裏的Content-Type 頭說明了這個。
由於咱們添加了JACKSON
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.5.3</version>
- </dependency
由於Spring在類路徑發現了這個庫,它調用了內置的
MappingJackson2HttpMessageConverter 轉換器將響應(對象集合)轉換爲JSON格式。
Spring內置轉換器的好處是,大部分狀況下只要把庫放到類路徑,便可完成轉換。固然了有時候咱們也須要
採用咱們的API。好比,若是咱們像也提供XML格式的話,咱們須要對User類加上JAXB註解。
2. 獲取單個用戶
GET方式 指定/user/1
如今試着發送一個帶有錯誤識別碼的GET請求,將收到一個HTTP 404
3.建立一個 User
選擇POST方法,指明uri /user/ 指明POSTMAN Body選項卡,選擇application/json類型
你要注意POSTMAN自動添加了Content-Type 頭信息
記住: Accept header包含client能給識別的類型。 Content-Type header表示數據的實際類型。
點擊發送之後 將收到 HTTP 200 沒有響應體(api裏面沒有在響應體發送任何東西)
你能夠查詢新建立的用戶
這是實現REST的普通實現方式。可是也沒人阻止你爲POST或者PUT方式響應體裏發送內容。可是這仍是REST 的API?值得懷疑。
無論怎樣,咱們試着建立同一個用戶時,你將得到HTTP衝突的響應。
4.更新用戶
發送一個HTTP PUT 請求來更新用戶。
注意:此次咱們接收到了響應體。這是由於在控制器的方法實現裏咱們發送了數據。再次強調,有的人也許不在響應體裏面發送更新的詳情,只發送位置頭(和建立用戶同樣)。
5.刪除用戶
6 刪除全部用戶
7.刪除用戶後驗證
根據RestTemplate 寫REST Client
Postman是測試Rest Api的超好用的工具,可是若是你想完整的消化REST,能夠嘗試本身寫一個。
最出名的Htpp 客戶端是HttpClient( Apache HttpComponents )。
可是用它來訪問REST service則相對少見。
Spring的
RestTemplate隨之出現。RestTemplate 提供了高級方法,來響應者6種主要的HTTP方法。
HTTP 方法和對應的 RestTemplate方法:
- HTTP GET : getForObject, getForEntity
- HTTP PUT : put(String url, Object request, String…urlVariables)
- HTTP DELETE : delete
- HTTP POST : postForLocation(String url, Object request, String… urlVariables), postForObject(String url, Object request, ClassresponseType, String… uriVariables)
- HTTP HEAD : headForHeaders(String url, String… urlVariables)
- HTTP OPTIONS : optionsForAllow(String url, String… urlVariables)
- HTTP PATCH and others : exchange execute
定義 Rest client , 定義REST services
- package com.websystique.springmvc;
-
- import java.net.URI;
- import java.util.LinkedHashMap;
- import java.util.List;
-
- import org.springframework.web.client.RestTemplate;
-
- import com.websystique.springmvc.model.User;
-
- public class SpringRestTestClient {
-
- public static final String REST_SERVICE_URI = "http://localhost:8080/Spring4MVCCRUDRestService";
-
-
- @SuppressWarnings("unchecked")
- private static void listAllUsers(){
- System.out.println("Testing listAllUsers API-----------");
-
- RestTemplate restTemplate = new RestTemplate();
- List<LinkedHashMap<String, Object>> usersMap = restTemplate.getForObject(REST_SERVICE_URI+"/user/", List.class);
-
- if(usersMap!=null){
- for(LinkedHashMap<String, Object> map : usersMap){
- System.out.println("User : id="+map.get("id")+", Name="+map.get("name")+", Age="+map.get("age")+", Salary="+map.get("salary"));;
- }
- }else{
- System.out.println("No user exist----------");
- }
- }
-
-
- private static void getUser(){
- System.out.println("Testing getUser API----------");
- RestTemplate restTemplate = new RestTemplate();
- User user = restTemplate.getForObject(REST_SERVICE_URI+"/user/1", User.class);
- System.out.println(user);
- }
-
-
- private static void createUser() {
- System.out.println("Testing create User API----------");
- RestTemplate restTemplate = new RestTemplate();
- User user = new User(0,"Sarah",51,134);
- URI uri = restTemplate.postForLocation(REST_SERVICE_URI+"/user/", user, User.class);
- System.out.println("Location : "+uri.toASCIIString());
- }
-
-
- private static void updateUser() {
- System.out.println("Testing update User API----------");
- RestTemplate restTemplate = new RestTemplate();
- User user = new User(1,"Tomy",33, 70000);
- restTemplate.put(REST_SERVICE_URI+"/user/1", user);
- System.out.println(user);
- }
-
-
- private static void deleteUser() {
- System.out.println("Testing delete User API----------");
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.delete(REST_SERVICE_URI+"/user/3");
- }
-
-
-
- private static void deleteAllUsers() {
- System.out.println("Testing all delete Users API----------");
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.delete(REST_SERVICE_URI+"/user/");
- }
-
- public static void main(String args[]){
- listAllUsers();
- getUser();
- createUser();
- listAllUsers();
- updateUser();
- listAllUsers();
- deleteUser();
- listAllUsers();
- deleteAllUsers();
- listAllUsers();
- }
- }
重啓服務器,運行上面的程序。
下面是輸出:
- Testing listAllUsers API-----------
- User : id=1, Name=Sam, Age=30, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=3, Name=Jerome, Age=45, Salary=30000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- Testing getUser API----------
- User [id=1, name=Sam, age=30, salary=70000.0]
- Testing create User API----------
- Location : http://localhost:8080/Spring4MVCCRUDRestService/user/5
- Testing listAllUsers API-----------
- User : id=1, Name=Sam, Age=30, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=3, Name=Jerome, Age=45, Salary=30000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- User : id=5, Name=Sarah, Age=51, Salary=134.0
- Testing update User API----------
- User [id=1, name=Tomy, age=33, salary=70000.0]
- Testing listAllUsers API-----------
- User : id=1, Name=Tomy, Age=33, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=3, Name=Jerome, Age=45, Salary=30000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- User : id=5, Name=Sarah, Age=51, Salary=134.0
- Testing delete User API----------
- Testing listAllUsers API-----------
- User : id=1, Name=Tomy, Age=33, Salary=70000.0
- User : id=2, Name=Tom, Age=40, Salary=50000.0
- User : id=4, Name=Silvia, Age=50, Salary=40000.0
- User : id=5, Name=Sarah, Age=51, Salary=134.0
- Testing all delete Users API----------
- Testing listAllUsers API-----------
- No user exist----------
完整的例子
更新pom.xml添加項目依賴
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.websystique.springmvc</groupId>
- <artifactId>Spring4MVCCRUDRestService</artifactId>
- <packaging>war</packaging>
- <version>1.0.0</version>
- <name>Spring4MVCCRUDRestService Maven Webapp</name>
-
- <properties>
- <springframework.version>4.2.0.RELEASE</springframework.version>
- <jackson.version>2.5.3</jackson.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${springframework.version}</version>
- </dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>${jackson.version}</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
-
- </dependencies>
-
-
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.4</version>
- <configuration>
- <warSourceDirectory>src/main/webapp</warSourceDirectory>
- <warName>Spring4MVCCRUDRestService</warName>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
-
- <finalName>Spring4MVCCRUDRestService</finalName>
- </build>
- </project>
User Service
- package com.websystique.springmvc.service;
-
- import java.util.List;
-
- import com.websystique.springmvc.model.User;
-
-
-
- public interface UserService {
-
- User findById(long id);
-
- User findByName(String name);
-
- void saveUser(User user);
-
- void updateUser(User user);
-
- void deleteUserById(long id);
-
- List<User> findAllUsers();
-
- void deleteAllUsers();
-
- public boolean isUserExist(User user);
-
- }
- package com.websystique.springmvc.service;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicLong;
-
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
-
- import com.websystique.springmvc.model.User;
-
- @Service("userService")
- @Transactional
- public class UserServiceImpl implements UserService{
-
- private static final AtomicLong counter = new AtomicLong();
-
- private static List<User> users;
-
- static{
- users= populateDummyUsers();
- }
-
- public List<User> findAllUsers() {
- return users;
- }
-
- public User findById(long id) {
- for(User user : users){
- if(user.getId() == id){
- return user;
- }
- }
- return null;
- }
-
- public User findByName(String name) {
- for(User user : users){
- if(user.getName().equalsIgnoreCase(name)){
- return user;
- }
- }
- return null;
- }
-
- public void saveUser(User user) {
- user.setId(counter.incrementAndGet());
- users.add(user);
- }
-
- public void updateUser(User user) {
- int index = users.indexOf(user);
- users.set(index, user);
- }
-
- public void deleteUserById(long id) {
-
- for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
- User user = iterator.next();
- if (user.getId() == id) {
- iterator.remove();
- }
- }
- }
-
- public boolean isUserExist(User user) {
- return findByName(user.getName())!=null;
- }
-
- private static List<User> populateDummyUsers(){
- List<User> users = new ArrayList<User>();
- users.add(new User(counter.incrementAndGet(),"Sam",30, 70000));
- users.add(new User(counter.incrementAndGet(),"Tom",40, 50000));
- users.add(new User(counter.incrementAndGet(),"Jerome",45, 30000));
- users.add(new User(counter.incrementAndGet(),"Silvia",50, 40000));
- return users;
- }
-
- public void deleteAllUsers() {
- users.clear();
- }
-
- }
Model (模型)類
- package com.websystique.springmvc.model;
-
- public class User {
-
- private long id;
-
- private String name;
-
- private int age;
-
- private double salary;
-
- public User(){
- id=0;
- }
-
- public User(long id, String name, int age, double salary){
- this.id = id;
- this.name = name;
- this.age = age;
- this.salary = salary;
- }
-
- public long getId() {
- return id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public double getSalary() {
- return salary;
- }
-
- public void setSalary(double salary) {
- this.salary = salary;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (int) (id ^ (id >>> 32));
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- User other = (User) obj;
- if (id != other.id)
- return false;
- return true;
- }
-
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", age=" + age
- + ", salary=" + salary + "]";
- }
-
-
- }
配置類
- package com.websystique.springmvc.configuration;
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
-
- @Configuration
- @EnableWebMvc
- @ComponentScan(basePackages = "com.websystique.springmvc")
- public class HelloWorldConfiguration {
-
-
- }
初始化類
- package com.websystique.springmvc.configuration;
-
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
-
- public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class[] { HelloWorldConfiguration.class };
- }
-
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return null;
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[] { "/" };
- }
-
- }
爲你的REST API添加CORS支持
當訪問REST API時,你可能須要面對「同源策略」問題。
錯誤以下:
」 No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://127.0.0.1:8080′ is therefore not allowed access.」 OR
」 XMLHttpRequest cannot load http://abc.com/bla. Origin http://localhost:12345 is not allowed by Access-Control-Allow-Origin.」
通常來講,在服務器端,咱們在響應中返回額外的CORS訪問控制頭,實現跨域連接。
用 Spring的話,我麼能夠寫一個簡單的過濾器爲每一個響應添加CORS特徵頭。
- package com.websystique.springmvc.configuration;
-
- import java.io.IOException;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletResponse;
-
-
- public class CORSFilter implements Filter {
-
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- System.out.println("Filtering on...........................................................");
- HttpServletResponse response = (HttpServletResponse) res;
- response.setHeader("Access-Control-Allow-Origin", "*");
- response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
- response.setHeader("Access-Control-Max-Age", "3600");
- response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
- chain.doFilter(req, res);
- }
-
- public void init(FilterConfig filterConfig) {}
-
- public void destroy() {}
-
- }
須要將其添加在Spring 配置中:
- package com.websystique.springmvc.configuration;
-
- import javax.servlet.Filter;
-
- import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
-
- public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class[] { HelloWorldConfiguration.class };
- }
-
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return null;
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[] { "/" };
- }
-
- @Override
- protected Filter[] getServletFilters() {
- Filter [] singleton = { new CORSFilter()};
- return singleton;
- }
-
- }