1.@Controller和@RestController的区别:
简单来说:
@RestController就是包括了@Controller和@ResponseBody两个注解 使用它只需要返回数据格式即可、例如json、xml
2.@RequestParam和@PathVariable的区别:
分别用这两个注解来做同一个后台接口、来比较它们的区别
@RequestParam:是获取的url拼接的的参数值作为后台参数
后台接口:
@GetMapping(value = "/books", produces = "application/json")
@ResponseBody
public Map<String, Object> findBookById(
@RequestParam(value = "name") String name
) {
List<Book> list = iMaintainBookService.findBook(name);
请求网址:
eg:http://localhost:8080/admin/books?name=西游
它是获取?后面name的值作为后台参数的值 url传参传的是键值对
需要注意的是:如何后台变量要求不和前台一致的话需要@requestparm的value属性的值要和前台传参的name属性一致
@PathVariable:是获取url路径中的变量作为参数
扫描二维码关注公众号,回复:
4928812 查看本文章
后台接口:
@GetMapping(value = "/books/{name}", produces = "application/json")
@ResponseBody
public Map<String, Object> findBookById(
@PathVariable("name") String name) {
eg:http://localhost:8080/admin/books/西游
其中name就是对应的请求路径的“西游”
比较二者:明显可以看出后者url路径更短一些 它需要在后台接口中添加传入的参数占位符:例如上诉 "{name}"