10.SpringBoot(接受参数相关注解)

常用注解

@PathVariable 路径参数获取信息

<a href="/monster/100/king">@PathVariable-路径变量:/monster/100/king</a>

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class ParameterController {

    @GetMapping("/monster/{id}/{name}") //接受两个路径参数
    public String pathVariable(@PathVariable("id") Integer id, @PathVariable("name") String name,
                               @PathVariable Map<String, String> map) { //这里的map指将所有的路径参数都放到map中
        System.out.println("id:" + id + " name:" + name);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key:" + entry.getKey() + " value: " + entry.getValue());
        }
        return "success"; //返回json给浏览器
    }

}

 @RequestHeader 请求头获取信息

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
public class ParameterController {

    @GetMapping("/requestHeader") //获取请求头的信息
    public String requestHeader(@RequestHeader("host") String host, @RequestHeader Map<String, String> header) {
        System.out.println("host:" + host);
        System.out.println(header);
        return "success";
    }

}

@RequestParameter 请求获取参数信息

<a href="/hi?hobby=打篮球&hobby=踢球">@RequestParam-请求参数</a>

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class ParameterController {

    @GetMapping("/hi")
    public String hi(@RequestParam(value = "name", defaultValue = "xxx") String name,
                     @RequestParam("hobby") List<String> list) {
        System.out.println("name:" + name);
        System.out.println(list);
        return "success";
    }


}

@CookieValue cookie获取值

<a href="/cookie">@CookieValue-获取cookie的值</a>

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@RestController
public class ParameterController {

    @GetMapping("/cookie")
    //这里可以设置required = false意为不是必须存在的,如果不存在则得到的值就为null
    //如果后面的参数类型是Cookie,则会获取Cookie对象并封装到变量中
    public String cookie(@CookieValue(value = "cookie_key", required = false) String cookie_value,
                         @CookieValue(value = "username" , required = false) Cookie cookie, HttpServletRequest request) {
        //使用原生api获取cookies
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println(cookie1);
        }

        System.out.println(cookie_value);
        System.out.println("name:" + cookie.getName() + " value: " + cookie.getValue());

        return "success";
    }


}

@RequestBody 处理json请求,post请求体获取信息

<form action="/requestBody" method="post">
    <input type="text" name="username"><br>
    <input type="text" name="password"><br>
    <input type="submit" value="submit">
</form>

@RestController
public class ParameterController {

    @PostMapping("requestBody")
    public String getRequestBody(@RequestBody String requestBody) { //获取请求体
        System.out.println(requestBody);
        return "success";
    }

}

@RequestAttribute 请求域获取信息

@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        //在Request域中存放一些信息
        request.setAttribute("name", "sun");
        request.setAttribute("age", 13);
        //调用视图解析器,请求转发到/ok
        return "forward:/ok";
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@RequestAttribute(value = "name", required = false) String name) { //使用注解来获取请求域中的信息并封装到参数中
        System.out.println("name: " + name);
        return "success"; //返回json给浏览器
    }
}

2.配置视图解析器 application.yml

spring:
  mvc:
    view: #配置了视图解析器
      suffix: .html #后缀
      prefix: / #前缀,指的是根目录

@SessionAttribute session域获取信息

@Controller
    public class SessionController {
    
        @GetMapping("/login")
        public String login(HttpServletRequest request) {
            //在session域中设置信息
            request.getSession().setAttribute("session", "session_value");
    
            //调用视图解析器,请求转发到/ok
            return "forward:/ok";
        }
    
        @ResponseBody
        @GetMapping("/ok")
        public String ok(@SessionAttribute(value = "session") String value) { //使用注解来获取session域中的信息并封装到参数中
            System.out.println("session: " + value);
            return "success"; //返回json给浏览器
        }
    }

2.配置视图解析器(同上)

复杂参数处理

@RequestAttribute

package com.sun.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(Map<String, Object> map, Model model, HttpServletResponse response) {
        //给map封装信息
        map.put("user", "sun");
        map.put("job", "工程师");

        //model封装信息
        model.addAttribute("sal", 1000);

        //结果最后都会封装到request域中

        //调用视图解析器,请求转发到/ok
        return "forward:/ok";
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@RequestAttribute("user") String user, @RequestAttribute("job") String job,
                     @RequestAttribute("sal") Integer sal) { //使用注解来获取请求域中的信息并封装到参数中
        System.out.println("user:" + user + " job:" + job + " sal:" +sal);
        return "success"; //返回json给浏览器
    }
}

@CookieValue

package com.sun.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;


@Controller
public class RequestController {

    @GetMapping("/login")
    public String login(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookie_name", "cookie_value");
        response.addCookie(cookie);

        //调用视图解析器,重定向到/ok,不能使用请求转发,因为虽然响应给客户端cookie了,
        // 但是由于是请求转发,第二个controller得到的是最开始的请求,那时候还没有cookie
        return "redirect:/ok";
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@CookieValue("cookie_name") Cookie cookie) {
        //获取cookie
        System.out.println("key:" + cookie.getName() + " value:" + cookie.getValue());
        return "success"; //返回json给浏览器
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值