Rest API请求参数与Spring后台常用接收参数注解的各种类型介绍。
- @RequestParam
- @RequestBody
- @PathVariable
- 实体Bean
- 数据类型
另外还有两个常用方法注解:
- @RequestMapping
- @Responsebody
@RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 Controller的方法上,作为请求的映射地址。
@ResponseBody是作用在方法上的,@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用【也就是AJAX】,在使用 @RequestMapping后,返回值通常解析为跳转路径,但是加上 @ResponseBody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。 比如异步获取 json 数据,加上 @ResponseBody 后,会直接返回 json 数据。@RequestBody 将 HTTP 请求正文插入方法中,使用适合的 HttpMessageConverter 将请求体写入某个对象。
标准restful api请求资源实例
以student实体为例:
/**
* Get请求
* @return
*/
@RequestMapping(value="student",method=RequestMethod.GET)
@ResponseBody
public Student read(Student student) {
return student;
}
/**
* POST请求
* @return
*/
@RequestMapping(value="student",method=RequestMethod.POST)
@ResponseBody
public int creat(Student student) {
return 0;
}
/**
* PUT请求
* @return
*/
@RequestMapping(value="student",method=RequestMethod.PUT)
@ResponseBody
public Student update(Student student) {
return student;
}
/**
* DELETE请求
* @return
*/
@RequestMapping(value="student",method=RequestMethod.DELETE)
@ResponseBody
public int delete(Student student) {
return 0;
}