- @Controller
- @RequestMapping
- @ResponseBody
- @PathVariable
- @RequestParam
- @RequestBody
- @RequestParam 和 @RequestBody 的区别
- JQuery ajax 请求的参数
@Controller
- 作用
标注当前类是一个控制类,类名不能和注解名一样
- 例子
@Controller // 标注这是一个控制类,类名不能和注解名一样
@RequestMapping("/book") // 访问父路径
public class BookController {
......
}
@RequestMapping
- 作用
RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
- 属性
RequestMapping 注解有六个属性
1.value:指定请求的实际地址
2.method:指定请求的 method 类型,GET、POST、PUT、DELETE 等
3.consumes:指定处理请求的提交内容类型(Content-Type),例如 application/json, text/html
4.produces:指定返回的内容类型,仅当 request 请求头中的(Accept)类型中包含该指定类型才返回
- 例子
用于类上,声明访问父路径
@RequestMapping("/book") // 访问父路径
public class BookController {
....
}
用于方法上,声明访问子路径
@RequestMapping(value = "/list", method = RequestMethod.GET)//访问子路径
private String list(Model model) {
List<Book> list = bookService.getList(0, 10);
model.addAttribute("list", list);
return "list";//此方法没有加@ResponseBody注解,返回的是一个前端页面
}
@RequestMapping(value = "/{bookId}/{studentId}/appoint", method = RequestMethod.GET, produces = {
"application/json; charset=utf-8" })
@ResponseBody
- 作用
该注解用于将 Controller 的方法返回的对象,通过适当的 HttpMessageConverter 转换为指定格式后,写入到 Response 对象的 body 数据区
- 使用时机
一般在异步获取数据时使用,在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @responsebody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。比如异步获取 json 数据,加上 @responsebody 后,会直接返回json数据
- 注意点
当前端使用 ajax 获取 controller 的数据时,此注解不能省略,否者ajax无法接受返回值
- 例子
有 @ResponseBody 注解,返回的是 json、xml 等格式的数据
@RequestMapping(value = "/{bookId}/{studentId}/appoint", method = RequestMethod.GET, produces = {
"application/json; charset=utf-8" })
@ResponseBody//此方法有@ResponseBody注解,返回的是json、xml等格式的数据
private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId,
@PathVariable("studentId") Long studentId) {
......
}
没有@ResponseBody注解,返回的是一个前端页面
@RequestMapping(value = "/list", method = RequestMethod.GET)//访问子路径
private String list(Model model) {
List<Book> list = bookService.getList(0, 10);
model.addAttribute("list", list);
return "list";//此方法没有加@ResponseBody注解,返回的是一个前端页面
}
@PathVariable
- 作用
映射 URL 绑定的占位符
- 例子
@RequestMapping(value = "/{bookId}/{studentId}/appoint", method = RequestMethod.POST, produces = {
"application/json; charset=utf-8" })
@ResponseBody
private Result<AppointExecution> appoint(@PathVariable("bookId") Long bookId,
@PathVariable("studentId") Long studentId) {
if(bookId == null || bookId.equals("")) {
return new Result<>(false,"图书编号不能为空");
}
if (studentId == null || studentId.equals("")) {
return new Result<>(false, "学号不能为空");
}
......
}
@RequestParam
- 作用
处理 Content-Type:application/x-www-form-urlencoded 编码的内容( Http 协议中,如果不指定 Content-Type,则默认传递的参数就是 application/x-www-form-urlencoded 类型)
JQuery 的 ajax 默认传递的是 application/x-www-form-urlencoded 类型的数据,如果想传递 json 类型,则需指定 contentType:”application/json; charset=utf-8”
- 原理
Request.getParameter() 中的 Key-Value 参数 Map 利用 Spring 的转化机制 ConversionService 配置,转化成参数接收对象或字段。
- 注意点
1.如果前端传过来的数据类型是 json 或者 xml,则此注解无法接收请求
2.可以通过 required=false 或者 true 来要求 @RequestParam 配置的前端参数是否一定要传
3.如果 @requestParam 注解的参数是 int 类型,并且 required=false,此时如果不传参数的话,会报错。原因是,required=false 时,不传参数的话,会给参数赋值 null,这样就会把 null 赋值给了 int,因此会报错。
- 例子
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
@ResponseBody//此注解不能省略,否则ajax无法接受返回值
private Result<BookExecution> insertBook(@RequestParam("name")String name, @RequestParam("number")Long number) {
if (name == null || name.equals("")) {
return new Result<>(false, "图书名字不能为空");
}
if (number == null || number < 0) {
return new Result<>(false, "馆藏数量不能为空或者小于0");
}
......
}
@RequestBody
- 作用
一般用来处理非 Content-Type: application/x-www-form-urlencoded 编码格式的数据
- 注意点
GET 请求中,因为没有 HttpEntity,所以 @RequestBody 并不适用
POST 请求中,通过 HttpEntity 传递的参数,必须要在请求头中声明数据的类型 Content-Type,SpringMVC 通过使用 HandlerAdapter 配置的 HttpMessageConverters 来解析 HttpEntity 中的数据,然后绑定到相应的 bean 上。
- 例子
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
@ResponseBody//此注解不能省略,否则ajax无法接受返回值
private Result<BookExecution> insertBook(@RequestBody EventData ev) {
if (ev.getName() == null || ev.getName().equals("")) {
return new Result<>(false, "图书名字不能为空");
}
if (ev.getNumber() == null || ev.getNumber() < 0) {
return new Result<>(false, "馆藏数量不能为空或者小于0");
}
......
}
@RequestParam 和 @RequestBody 的区别
-
在 GET 请求中,不能使用 @RequestBody
-
在 POST 请求,可以使用 @RequestBody 和 @RequestParam ,但是请注意 @RequestParam 处理 Content-Type:application/x-www-form-urlencoded 编码,@RequestBody 处理非 Content-Type:application/x-www-form-urlencoded 编码
JQuery ajax 请求的参数
- contentType: 告诉服务器,我要发什么类型的数据
如:contentType:”application/json; charset=utf-8”
- dataType:告诉服务器,我要想什么类型的数据。如果没有指定,那么会自动推断是返回 XML,还是 JSON ,还是 script ,还是 String。
如:dataType: “json”