REST(Representational State Transfer),表现形式状态转换,是一种资源描述形式
根据REST风格对资源进行访问称为RESTful
优点:
- 隐藏资源的访问行为,无法通过地址得知对资源是何种操作
- 书写简化
示例
@PostMapping
public String save(@RequestBody Book book) {
System.out.println("book save..." + book);
return "{'module':'book save'}";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
System.out.println("book delete..." + id);
return "{'module':'book delete'}";
}
@PutMapping
public String update(@RequestBody Book book) {
System.out.println("book update..." + book);
return "{'module':'book update'}";
}
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("book getById..." + id);
return "{'module':'book getById'}";
}
@GetMapping
public String getAll() {
System.out.println("book getAll...");
return "{'module':'book getAll'}";
}
在Postman的请求发送类型
注解解释
名称:@RequestMapping
作用:设置当前控制器方法请求访问路径
value(默认):请求访问路径
method:http请求动作,标准动作(GET/POST/PUT/DELETE)
@RequestMapping(value = "/users", method = RequestMethod.POST)
名称:@PathVariable
作用:绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id) {
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@RequestBody @RequestParam @PathVariable
区别:
- @RequestParam用于接收url地址传参或表单传参
- @RequestBody用于接收json数据
- @PathVariable用于接收路径参数,使用{参数名称}描述路径参数
应用: - 后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
- 如果发送非json格式数据,选用@RequestParam接收请求参数
- 采用RESTful进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值
RESTful快速开发
前面的注解开发还是有点繁杂,基于RESTful的快速开发诞生,主要是合并注解
- @ResponseBody不放在方法中,放在类上方,在整个类中都生效
@Controller与@ResponseBody
-->@RestController
@RequestMapping(value = "/users/{id}", method = RequestMethod.Get)
-->@GetMapping("/{id}")
- 共有@GetMapping @PostMapping @PutMapping @DeleteMapping
案例
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String save(@RequestBody Book book) {
System.out.println("book save " + book);
return "{'module':'book save success'}";
}
@GetMapping
public List<Book> getAll() {
ArrayList<Book> bookList = new ArrayList<Book>();
Book book1 = new Book();
book1.setType("软件工程");
book1.setName("SpringMVC");
book1.setDescription("小试牛刀");
bookList.add(book1);
return bookList;
}
}
在快速开发中,要设置对静态资源的访问放行
@Configuration
public class SpringMvcSupports extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/xxxx不要走mvc,而是走/pages目录下的内容
// registry.addResourceHandler("/webapp/**").addResourceLocations("/webapp/");
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
REST风格使得开发有了更好的规范
标签:return,PathVariable,REST,id,book,开发,风格,println,public From: https://www.cnblogs.com/llg-/p/17370739.html