REST
Representational State Transfer:资源表现层状态转换,是目前比较主流的一种互联网软件架构,它的结构清晰、标准规范、易于理解、便于扩展。
资源(Resource)
网络上的一个实体,或者硕网络上中存在的一个具体的信息,一个文本,一张图片、一首歌曲、一段视频等等,总之就是一个具体的存在。可以用一个URL(统一资源定位符)指向它,每个资源都有对应的特定的URL,要获取该资源时,只需要访问对应的URL即可。
表现层(Representation)
资源的具体呈现出来的形式,必须文本可以用txt格式表示,也可以用HTML\JSON\XML等格式来表示。
状态转换(StateTransfer)
客户端如果希望操作服务器中的某个资源,就需要通过某种方式让服务端发生状态转换,而这种转换是建立在表现层之上的,所以叫作做“表现层状态转换”。
SpringMVC REST特点
- URL更加简洁
- 有利于不同系统之间的资源共享,只需要遵守一定的规范,不需要进行其他的配置就可以实现资源共享。
如何使用
REST具体操作就是HTTP协议中四个表示操作方式的动词分别对应CRUD基本操作。
GET 用来表示获取资源
POST 用来表示新建资源
PUT 用来表示修改资源
DELETE 用来表示删除资源
在StudentRepository接口中定义一个Map集合,用来存放Student的信息模拟数据库,在StudentRspository接口的实现类中添加学生的信息,在Handler中写增删改查的业务方法。增删改的具体操作在postman中手动实现。在postman中,如果要实现save和update方法,不是只在postman中的地址栏输入http:/localhost:8080/rest/save或者http:/localhost:8080/rest/update,不然会报错:The request sent by the client was syntactically incorrect
另外向客户端传入的是一个json对象,所以要在pom.xml中添加相关json依赖,另外在springmvc,xml中,在mvc驱动中也要添加响应的bean。不然SpringMVC返回对象类型报错:HttpMessageNotWritableException: No converter found for return value of type
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
springmvc.xml
<mvc:annotation-driven conversion-service="conversionService">
<!-- 消息转换器(在注解驱动里面加) -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"></property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</mvc:message-converters>
</mvc:annotation-driven>
Handler业务代码
@RestController
@RequestMapping("/rest")
public class RESTHandler {
//根据对象类型来将对象注入IOC容器
@Autowired
private StudentRepository studentRepository;
// @RequestMapping(value = "/findAll",method = RequestMethod.GET)
//该注解同上
@GetMapping("/findAll")
public Collection<Student> findAll(HttpServletResponse response){
//在springmvc.xml中我们已经配置了解决的中文乱码的问题;但是如果返回客户端的是一个对象,就要在此设置对象中的中文乱码的问题
response.setContentType("text/json;charset=UTF-8");
return this.studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") Integer id){
return studentRepository.findById(id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") Integer id){
studentRepository.deleteById(id);
}
}
StudentRepository接口
public interface StudentRepository {
public Collection<Student> findAll();
public Student findById(Integer id);
public void saveOrUpdate(Student student);
public void deleteById(Integer id);
}
StudentRepository接口的实现类StudentRespositoryImpl
@Repository
public class StudentRepositoryImpl implements StudentRepository{
//定义静态集合,来模拟数据库
private static Map<Integer, Student> studentMap;
//在静态代码块中实例化该集合
static {
studentMap = new HashMap<>();
studentMap.put(1,new Student(1,"张三",98.3));
studentMap.put(2,new Student(2,"李四",97.9));
studentMap.put(3,new Student(3,"王五",99.6));
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(Integer id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(Integer id) {
studentMap.remove(id);
}
}