java篇-spingboot web应用注解

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

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值