Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)
在这篇文章中,我们将使用Spring4 MVC编写一个CRUD RESTful Web服务,写一个REST客户端RestTemplate来使用这些服务。我们也将利用外部客户端测试的服务。
下面将展示核心代码,更详细的代码实现参照Maven示例工程!
简单介绍REST
REST表示状态传输。这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗。核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调用。
-
创建资源:应该使用 HTTP POST
-
要获取资源:应该使用HTTP GET
-
更新资源:应使用HTTP PUT
-
要删除资源:应使用HTTP DELETE
通常REST是基于Web服务返回JSON或XML数据格式作为响应,虽然它并不仅仅限于这些类型。客户端可以指定(使用 HTTP Accept 报头),他们所感兴趣的资源类型,并且服务器可以返回资源,指定它所服务的内容类型资源。
下面是一个可能基于REST的控制器,实现REST API。这里所说的“可能”,这意味着可以以另一种方式实现它,还是(或者更纯粹的方式)符合REST风格。
- GET请求/api/user/返回用户的列表
- GET请求/api/user/1返回ID为1的用户
- POST请求/api/user/以用户对象的JSON格式创建新的用户
- PUT请求/api/user/3以用户对象作为JSON更新ID为3的用户
- DELETE请求/api/user/4删除ID为4的用户
- DELETE请求/api/user/删除所有的用户
package com.jsoft.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.yiibai.springmvc.model.User; import com.yiibai.springmvc.service.UserService; @RestController public class HelloWorldRestController { @Autowired UserService userService; //Service which will do all data retrieval/manipulation work //-------------------Retrieve All Users-------------------------------------------------------- @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);//You many decide to return HttpStatus.NOT_FOUND } return new ResponseEntity<List<User>>(users, HttpStatus.OK); } //-------------------Retrieve Single User-------------------------------------------------------- @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); } //-------------------Create a User-------------------------------------------------------- @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); } //------------------- Update a User -------------------------------------------------------- @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); } //------------------- Delete a User -------------------------------------------------------- @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); } //------------------- Delete All Users -------------------------------------------------------- @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:首先,我们使用Spring4的新@RestController注释。 它的注解消除了注释每个以@ResponseBody的方法。@RestController本身注解为@ResponseBody,并且可以被视为@Controller和@ResponseBody的组合。
@RequestBody:如果一个方法的参数都注解有@RequestBody,Spring将绑定传入的HTTP请求体(在@RequestMapping提到该法的URL)到这个参数。这样做Spring将在后台使用HTTP消息转换为HTTP请求主体转换成域对象反序列化要求主体域对象的基础上,接受或Content-Type头请求。
@ResponseBody:如果一个方法被注解为@ResponseBody,Spring将绑定返回值传出的HTTP响应体。这样做Spring将在后台使用HTTP消息转换器的返回值转换为HTTP响应体[序列化对象响应正文,根据内容类型出现在请求的HTTP头。 前面已经提到,在Spring4可能会停止使用此注释。
ResponseEntity:是一个真正处理。 它代表了整个HTTP响应。一件好事是你可以控制任何进入它东西。可以指定状态码,头和主体。它自带几个构造函数执行你想要的 HTTP 响应发送的信息。
@PathVariable这种表示法表示方法参数应绑定到一个URI模板变量“{}”。
基本上,@RestController,@RequestBody,ResponseEntity,@PathVariable都是用Spring 4实现REST API需要知道的。此外,Spring提供了一些支持类来帮助你实现一些定制。
MediaType:通过@RequestMapping注解,你还可以,指定要生产或消费的MediaType(使用生产或消费属性),通过特定的控制器的方法,以进一步缩小映射。
我们上面使用Postman是一个很好的客户端测试REST API工具。但是,如果想从应用程序消耗基于REST的Web服务,需要一个REST客户端的应用程序。其中最流行的HTTP客户端就是Apache HttpComponents HttpClient。 但在细节上使用这种访问REST服务太低级。
Spring RestTemplate可以来补救。RestTemplate提供对应于六个主要的HTTP方法,使许多调用RESTful服务只需要一行代码,就可执行REST最佳实践的更高层次的方法。
下面显示的是HTTP方法和相应RestTemplate方法来处理该类型的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, Class responseType, String… uriVariables)
- HTTP HEAD : headForHeaders(String url, String… urlVariables)
- HTTP OPTIONS : optionsForAllow(String url, String… urlVariables)
- HTTP PATCH and others : exchange execute
package com.jsoft.springmvc; import java.net.URI; import java.util.LinkedHashMap; import java.util.List; import org.springframework.web.client.RestTemplate; import com.yiibai.springmvc.model.User; public class SpringRestTestClient { public static final String REST_SERVICE_URI = "http://localhost:8080/Spring4MVCCRUDRestService"; /* GET */ @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----------"); } } /* GET */ 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); } /* POST */ 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()); } /* PUT */ 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); } /* DELETE */ private static void deleteUser() { System.out.println("Testing delete User API----------"); RestTemplate restTemplate = new RestTemplate(); restTemplate.delete(REST_SERVICE_URI+"/user/3"); } /* DELETE */ 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----------
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/Spring4MVCCRUDRestService
参考:
http://www.yiibai.com/spring_mvc/spring-mvc-4-restful-web-services-crud-example-resttemplate.html(以上内容转自此篇文章)