spingboot web应用注解讲解
1、注解 Controller + RequestMapping
请求:localhost:8080/t1 ,返回的时页面的跳转链接,不是返回字符串。
@Controller
public class ParameterTestController {
@RequestMapping("/t1")
public String sayHello(){
return "t1";
}
}
2、注解 Controller + RequestMapping + ResponseBody
表示返回字符串
@Controller
public class ParameterTestController {
@ResponseBody
@RequestMapping("/t1")
public String sayHello(){
return "t1";
}
}
3、注解 RestController + RequestMapping
@RestController
是@ResponseBody
和@Controller
的组合注解。
@RestController
public class ParameterTestController {
@RequestMapping("/t1")
public String sayHello(){
return "t1";
}
}
请求:localhost:8080/t1 ,返回的字符串"t1"。
4、注解 RequestMapping("/t1") 配置在类上,表示路径的一部分
@RestController
@RequestMapping("/t1")
public class ParameterTestController {
@RequestMapping(value = "/t2", method= RequestMethod.GET)
public String sayHello(){
return "t1/t2";
}
}
请求地址:localhost:8080/t1/t2
5、注解PathVariable
获得url中的路径参数id,传给carId
@RestController
public class ParameterTestController {
@GetMapping(value = "/car/{id}/owner/{uname}")
public Map<String, Object> getCar(@PathVariable("id") int carId,
@PathVariable("uname") String username,
@PathVariable Map<String,String> pv) {
Map<String,Object> map1 = new HashMap<>();
map1.put("catid",carId);
map1.put("name",username);
map1.put("map1",pv);
return map1;
//http://localhost:8080/car/1/owner/zhangsan
}
请求路径:localhost:8080/car/1/owner/zhangsan
6、注解RequestHeader
@GetMapping(value = "/car/{id}/owner/{uname}")
public Map<String, Object> getCar(@RequestHeader("User-Agent") String agent,
@RequestHeader Map<String,String> headers) {
Map<String,Object> map1 = new HashMap<>();
map1.put("agent",agent);
map1.put("headerssss",headers);
return map1;
获取request的header中的一个属性或所有属性。
@RequestHeader("User-Agent") String agent, ----获取请求头的User-Agent属性,传给agent字段
@RequestHeader Map<String,String> headers ----获取请求头的所有,传给headers字段。
7、注解RequestParam
请求url:localhost:8080/car?age=1&inter=bask&inter=football
@RequestParam("age") Integer ageage: 获取get的请求参数age的值,传给ageage
@RequestParam("inter") List<String> inters:获取get的请求参数inter的值,传给inters
@RequestParam Map<String,Object> param:获取get的请求的所有参数,传给param(如果参数数时inter=bask&inter=football内容时,值保存第一个参数)
@GetMapping(value = "/car")
public Map<String, Object> getCar(@RequestParam("age") Integer ageage,
@RequestParam("inter") List<String> inters,
@RequestParam Map<String,Object> param) {
Map<String,Object> map1 = new HashMap<>();
map1.put("age",ageage);
map1.put("inters",inters);
map1.put("param",param);
return map1;
url请求返回结果为:
8、注解CookieValue
@CookieValue("_ga") String _ga ---过的cookie的名称
@CookieValue("_ga") Cookie cookies ---获得cookie的对象
System.out.println(cookies.getName()+"--"+cookies.getValue());--通过cookie对象获取cookie的name和value。
@GetMapping(value = "/car")
public Map<String, Object> getCar(@CookieValue("_ga") String _ga,
@CookieValue("_ga") Cookie cookies) {
Map<String, Object> map1 = new HashMap<>();
map1.put("_ga",_ga);
System.out.println(cookies);
System.out.println(cookies.getName()+"--"+cookies.getValue());
return map1;
执行结果:
System.out.println(cookies);
System.out.println(cookies.getName()+"--"+cookies.getValue());
结果:
javax.servlet.http.Cookie@358e8540
_ga--GA1.1.1715754369.1709088080
9、注解RequestBody(post请求的请求体)
@RestController
public class RequestBodyController {
@RequestMapping("/save")
public Map content(@RequestBody String content) {
Map<String, Object> map1 = new HashMap<>();
map1.put("content", content);
System.out.println(map1.toString());
return map1;
//http://localhost:8080/save
}
}
返回结果:
10、注解RequestAttribute
注解RequestAttribute
注解MatrixVariable