【归纳总结】SpringMVC之RESTful风格接口

与Json数据做交互

1️⃣ @PathVariable(重要)

获得请求url的一部分作为形参

    //@PathVariable注解的value属性值→占位符的名字
    //@PathVariable注解对应的形参就可以获得对应的占位符的url的值
    @RequestMapping("{usernamez}/article/details/{idz}")
    public BaseRespVo articleDetails(@PathVariable("usernamez") String username,
                                     @PathVariable("idz") Integer id){
        return BaseRespVo.ok(username + ":" + id);
    }

2️⃣ @RequestParam

获得请求参数作为形参

    //localhost:8080/login?username=octavius&password=123456
    @RequestMapping("login")
    public BaseRespVo login(@RequestParam("username") String parameter1,
                            @RequestParam("password") String parameter2){
        return BaseRespVo.ok();
    }

3️⃣ @RequestHeader

拿到特定的请求头中的值

    @RequestMapping("header")
    public BaseRespVo header(@RequestHeader("Accept") String[] accept,
                             @RequestHeader("Host")String host){
        for (String s : accept) {
            System.out.println(s);
        }
        System.out.println(host);
        return BaseRespVo.ok();
    }

4️⃣ @CookieValue

拿到cookie中指定name所对应的value

    @RequestMapping("cookie/value")
    public BaseRespVo cookieValue(@CookieValue("jingtian")String value){
        return BaseRespVo.ok(value);
    }

5️⃣ @SessionAttribute

拿到session中指定attributename对应的value

    @RequestMapping("put/session/{username}")
    public BaseRespVo putSession(@PathVariable("username")String value, HttpSession session){
        session.setAttribute("username",value);
        return BaseRespVo.ok();
    }

    @RequestMapping("session/attribute")
    public BaseRespVo sessionAttribute(@SessionAttribute("username")String value){
        return BaseRespVo.ok(value);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值