Spring4 MVC RESTFul WebServices CRUD实例+RestTemplate
简单介绍REST
REST 表示状态传输。这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗。核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调用。
在基于 REST 的设计中,资源被一套通用动词操作使用。
-
创建资源:应该使用 HTTP POST
-
要获取资源:应该使用HTTP GET
-
更新资源:应使用HTTP PUT
-
要删除资源:应使用HTTP DELETE
这意味着,作为一个 REST 服务开发人员或客户端,应符合上述标准,以便 REST 操作。
通常Rest 是基于Web服务返回JSON或XML数据格式作为响应,虽然它并不仅仅限于这些类型。客户端可以指定(使用 HTTP Accept 报头),他们所感兴趣的资源类型,并且服务器可以返回资源,指定它所服务的内容类型资源。
基于REST的控制器
下面是一个可能基于 REST 的控制器,实现REST API。这里所说的“可能”,这意味着可以以另一种方式实现它,还是(或者更纯粹的方式)符合REST风格。
这就是我们的 REST API 功能/作用:
- 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/ 删除所有的用户
需要用到的java包
java对象和json相互转化
jackson-mapper-asl jackson-databind
pom.xml
<properties> <springVersion>4.3.5.RELEASE</springVersion> <jacksonVersion>2.7.4</jacksonVersion> </properties> <dependencies> <!-- spring-test支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springVersion}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- spring模块库 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springVersion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springVersion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springVersion}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springVersion}</version> </dependency> <!-- Servlet dependencies --> <!-- servlet(HttpServletRequest,HttpServletResponse) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl --> <!-- java对象和json相互转化 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <warName>gugua11</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </pluginManagement> <finalName>gugua11</finalName> </build>
user字段结构
user.java
package 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; } }
userService接口
package springmvc.service; import java.util.List; import springmvc.model.User; public interface UserService { User findById(long id); User findByName(String name); void saveUser(User user); void updateUser(User usre); void deleteUserById(long id); List<User> findAllUser(); void deleteAllUser(); public boolean isUserExists(User user); }
userServiceImplement接口实现
@Service("userService") #声明服务,用于自动装配
@Transactional #事务支持(类似于msql事务提交)
package 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 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(); } @Override public List<User> findAllUser() { // TODO Auto-generated method stub return users; } @Override public User findById(long id) { // TODO Auto-generated method stub for(User user: users) { if(user.getId() == id) { return user; } } return null; } @Override public User findByName(String name) { // TODO Auto-generated method stub for(User user: users) { if(user.getName().equals(name) ) { return user; } } return null; } @Override public void saveUser(User user) { // TODO Auto-generated method stub user.setId(counter.incrementAndGet()); users.add(user); } @Override public void updateUser(User user) { // TODO Auto-generated method stub int index = users.indexOf(user); users.set(index, user); } @Override public void deleteUserById(long id) { // TODO Auto-generated method stub for(Iterator<User> iterator = users.iterator(); iterator.hasNext();) { User user = iterator.next(); if(user.getId() == id) { iterator.remove(); } } } @Override public boolean isUserExists(User user) { // TODO Auto-generated method stub return findByName(user.getName())!=null; } @Override public void deleteAllUser() { // TODO Auto-generated method stub users.clear(); } //默认数据 public static List<User> populateDummyUsers() { List<User> users = new ArrayList<User>(); users.add(new User(counter.incrementAndGet(), "张三", 30, 700000)); users.add(new User(counter.incrementAndGet(), "李四", 35, 850000)); users.add(new User(counter.incrementAndGet(), "王五", 36, 900000)); users.add(new User(counter.incrementAndGet(), "田七", 38, 1000000)); return users; } }
输出头部信息
CORSFilter.java
package 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 { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub System.out.print("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); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } @Override public void destroy() { // TODO Auto-generated method stub } }
springmvc,自动引入,配置声明
package 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="springmvc") public class HelloWorldConfiguration { }
初始分Dispatcher转发功能
package springmvc.configuration; import javax.servlet.Filter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { // TODO Auto-generated method stub return new Class[] { HelloWorldConfiguration.class }; } @Override protected Class<?>[] getServletConfigClasses() { // TODO Auto-generated method stub return null; } @Override protected String[] getServletMappings() { // TODO Auto-generated method stub return new String[]{"/"}; } @Override protected Filter[] getServletFilters() { Filter [] singleton = { new CORSFilter()}; return singleton; } }
控制器
package 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 springmvc.model.User; import springmvc.service.UserService; @RestController public class HelloWorldRestController { @Autowired UserService userService; @RequestMapping(value="/users", method=RequestMethod.GET) public ResponseEntity<List<User>> listAllUsers() { List<User> users = userService.findAllUser(); 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.PUT, produces=MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<User> getUser(@PathVariable("id") long id) { User user = userService.findById(id); if(user == null) { return new ResponseEntity<User>(HttpStatus.NOT_FOUND); } return new ResponseEntity<User>(user, HttpStatus.OK); } @RequestMapping(value="/user/{name}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<User> getNameUser(@PathVariable("name") String name) { User user = userService.findByName(name); if(user == null) { return new ResponseEntity<User>(HttpStatus.NOT_FOUND); } return new ResponseEntity<User>(user, HttpStatus.OK); } @RequestMapping(value="/user/create", method=RequestMethod.POST) public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder uriBuilder) { if(userService.isUserExists(user)) { return new ResponseEntity<Void>(HttpStatus.CONFLICT); } userService.saveUser(user); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uriBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri()); return new ResponseEntity<Void>(headers, HttpStatus.CREATED); } }
@RequestMapping(value="/user/delete/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<User> deleteUserOne(@PathVariable("id") long id) { User user = userService.findById(id); if(user == null) { return new ResponseEntity<User>(HttpStatus.NOT_FOUND); } userService.deleteById(id); return new ResponseEntity<User>(HttpStatus.NO_CONTENT); } @RequestMapping(value="/user/delete/all", method=RequestMethod.GET) public ResponseEntity<User> deleteAllUsers() { userService.deleteAllUser(); return new ResponseEntity<User>(HttpStatus.NO_CONTENT); }
大致访问:
http://localhost:8080/gugua11/users
http://localhost:8080/gugua11/user/1
......
用火狐插件RESTClient,注意添加HTTP头字段:
Content-Type: application/json