REST风格-简化mvc注解
注解
@RestController // 类注解 简化 等同于@Controller与@ResponseBody两个注解组合功能
方法的增删改优化注解
@GetMapping //方法上 查询操作
@PostMapping //方法上 增加操作
@PutMapping //方法上 修改操作
@DeleteMapping //方法上 删除操作
(@PathVariable Integer id) //形参注解 出现在根据id删除和根据id查询中1.REST介绍
传统风格资源描述形式
http://localhost/user/getById?id=1
http://localhost/user/saveUser
REST风格描述形式
http://localhost/user/1
http://localhost/user2.优点
1.隐藏资源的访问行为,无法通过地址得知对资源是何种操作
2.书写简化例:基于RESTful页面数据交互
//POJO实体类
public class Book {
private Integer id;
private String type;
private String name;
private String description;
//同学们自己重写getter、setter、toString()方法...
}
//SpringMVC容器初始化类
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
//SpringMVC配置类
@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc //在配置类 开启json数据类型自动转换
public class SpringMvcConfig {
}2.controller层
@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(){
System.out.println("book getAll is running ...");
List<Book> bookList = new ArrayList<Book>();
Book book1 = new Book();
book1.setType("计算机");
book1.setName("SpringMVC入门教程");
book1.setDescription("小试牛刀");
bookList.add(book1);
Book book2 = new Book();
book2.setType("计算机");
book2.setName("SpringMVC实战教程");
book2.setDescription("一代宗师");
bookList.add(book2);
Book book3 = new Book();
book3.setType("计算机丛书");
book3.setName("SpringMVC实战教程进阶");
book3.setDescription("一代宗师呕心创作");
bookList.add(book3);
return bookList;
}
}3.设置对静态资源的访问放行(配置静态资源路径)
//在springcongfig包下创建静态资源的访问放行
@Configuration //右键o resource add
public class SpringMvcSupport extends WebMvcConfigurationSupport {
//设置静态资源访问过滤,当前类需要设置为配置类,并被扫描加载
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/????时候,从/pages目录下查找内容
registry.addResourceHandler("/pages/**")
.addResourceLocations("/pages/");
registry.addResourceHandler("/js/**")
.addResourceLocations("/js/");
registry.addResourceHandler("/css/**")
.addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**")
.addResourceLocations("/plugins/");
}
}4.前端页面通过异步提交访问后台控制器
//添加
saveBook () {
axios.post("/books",this.formData).then((res)=>{
});
},
//主页列表查询
getAll() {
axios.get("/books").then((res)=>{
this.dataList = res.data;
});
},
使用REST风格简化SpringMVC控制器,
文章介绍了如何使用RESTful风格来设计URL,以及SpringMVC中的注解如@RestController、@GetMapping、@PostMapping等来简化控制器代码。示例展示了如何实现书籍资源的增删查改操作,并配置了静态资源的访问。此外,还提到了前端通过异步请求与后台交互。
8071

被折叠的 条评论
为什么被折叠?



