1.@RequestParam
@RequestParam用于接收参数,使用该注解的参数,传参的时候是必传的;当然也可以设置非必传。另外可以通过设置value值,改变url中传参的名字不必和方法中参数一致。
具体使用方法参考:https://jingyan.baidu.com/article/546ae185c816641148f28c66.html
2.@RequestBody
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)。
public String test(@RequestBody String jsonString)
具体使用方法参考:https://blog.csdn.net/justry_deng/article/details/80972817
3.@ResponseBody
@ResponseBody表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,用于构建RESTful的api。在使用@RequestMapping后,返回值通常解析为跳转路径,加上@ResponseBody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。比如异步获取json数据,加上@ResponseBody后,会直接返回json数据
package com.qijie.demo.zqjhttpclient.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class AnnotationController {
@RequestMapping("/test")
@ResponseBody
public String test(){
return "OK";
}
}
4.@RestController
@RestController:用于标注控制层组件(如struts中的action),相当于@RequestBody和@Controller的合集