简单介绍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

Spring4 MVC RESTFul WebServices CRUD实例+RestTemplate的更多相关文章

  1. Spring4 MVC ContentNegotiatingViewResolver多种输出格式实例

    本文演示支持多种输出格式,这里 Spring4 MVC应用程序使用了 Spring ContentNegotiatingViewResolver .我们将生成应用程序输出XML,JSON,PDF,XL ...

  2. Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)

    在这篇文章中,我们将使用Spring4 MVC编写一个CRUD RESTful Web服务,写一个REST客户端RestTemplate来使用这些服务.我们也将利用外部客户端测试的服务. 下面将展示核 ...

  3. Spring4 MVC+ AngularJS CRUD使用$http实例

    这篇文章显示了使用Spring MVC4整合AngularJS.我们将创建一个使用后端和AngularJS作为前端的纯JSP封装Spring REST API一个CRUD应用程序, 使用 $http ...

  4. [翻译]Spring MVC RESTFul Web Service CRUD 例子

    Spring MVC RESTFul Web Service CRUD 例子 本文主要翻译自:http://memorynotfound.com/spring-mvc-restful-web-serv ...

  5. Spring4 MVC HelloWorld 注解和JavaConfig实例

    在这一节中,我们以 Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 在先前的 Sp ...

  6. 【SpringMVC】SpringMVC系列11之Restful的CRUD

      11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除     优雅的 REST 风格的资 ...

  7. Spring FrameWork4(MVC + IOC)高速入门实例

    使用Maven创建project并配置依赖项 首先创建一个Maven Project: 然后选择创建Maven 的webapp实例,当然也能够通过命令行方式创建Maven webapp的项目再转化并导 ...

  8. Spring4 MVC ContentNegotiatingViewResolver多种输出格式实

    前段时间在一个项目里面发现,针对Excel的处理没有一个公用的视图,来个下载的需求就要自己去写一堆POI的东西,终于有一天给我也来了几个,还是按照以前的方式来写,写多了真心想吐,后面想想还是有必要整个 ...

  9. PHP curl 实现RESTful PUT DELETE 实例

    PHP curl 实现RESTful PUT DELETE 实例 客户端 client.php <?php //PUT $curl_handle = curl_init ();// Set de ...

随机推荐

  1. 静态绑定 self 和 static的区别

    后期静态绑定 自 PHP 5.3.0 起,PHP 增加了一个叫做后期静态绑定的功能,用于在继承范围内引用静态调用的类. 准确说,后期静态绑定工作原理是存储了在上一个“非转发调用”(non-forwar ...

  2. CodeForces 19D Points (线段树+set)

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  3. 快速定位oracle故障-恩墨

    首先我们要明白一点,所谓的故障,意味着相对来讲比较严重.也就是可能比不同的问题要严重一些,比如锁等待. 要能够快速的定位和解决问题,恢复业务正常:首先我们需要了解Oracle的一些常见的故障有哪些. ...

  4. 新一代Java模板引擎Thymeleaf

    新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...

  5. 电子商务(电销)平台中订单模块(Order)数据库设计明细

    电子商务(电销)平台中订单模块(Order)数据库设计明细 - sochishun - 博客园 http://www.cnblogs.com/sochishun/p/7040628.html 电子商务 ...

  6. 过山车---hdu2063(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063最大匹配模板题: #include <iostream> #include <c ...

  7. laydate设置起始时间,laydate设置开始时间和结束时间

    //设置开始时间 var startDate = laydate.render({ elem: '#start_date',//开始时间选择控件id min:'2018-6-1', type: 'da ...

  8. PyCharm安装与配置,python的Hello World

    1. 访问https://www.jetbrains.com/zh/pycharm/download/download-thanks.html, 下载pycharm 安 装包,点击安装. 2. 用记事 ...

  9. mysql 大表优化

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 转自:https://segmentfault.com/a/1190000006158186 单表优化 除非单表数据未 ...

  10. Hadoop集群完全分布式坏境搭建

    前言 上一篇我们讲解了Hadoop单节点的安装,并且已经通过VMware安装了一台CentOS 6.8的Linux系统,咱们本篇的目标就是要配置一个真正的完全分布式的Hadoop集群,闲言少叙,进入本 ...