SpringBoot中SpringMVC的基本注解使用

0. servlet-context-path

Servlet的默认初始前缀

server.servlet.context-path=/SpringMVCLearning

1. @RequestMapping 请求映射

@RequestMapping 源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};
    @AliasFor("value")
    String[] path() default {};
    RequestMethod[] method() default {};
    String[] params() default {};
    String[] headers() default {};
    String[] consumes() default {};
    String[] produces() default {};
}

用来映射请求

@RestController
@RequestMapping("/annotationTest")
public class AnnotationTestController {

    @RequestMapping(value = "/requestMapping1", method = RequestMethod.GET)
    public String requestMappingTest1() {
        return "RequestMapping1";
    }
}

访问 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/requestMapping1 得到结果:
RequestMapping1


1.1 Ant风格映射

? 匹配一个字符
* 匹配文件名中的任意字符
** 匹配多重路径
示例:

@RequestMapping(value = "/antMapping1/**", method = RequestMethod.GET)
    public String antMappingTest(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        return requestURI;
    }

访问 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/antMapping1/hahaha 得到结果:
/SpringMVCLearning/annotationTest/antMapping1/hahaha


2. @RequestParam 获取请求参数

@RequestParam 源码:

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    @AliasFor("name")
    String value() default "";
    @AliasFor("value")
    String name() default "";

    boolean required() default true;

    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

2.1 获得具体的请求参数

示例:

@RequestMapping(value = "/requestParam")
    public String requestParam(@RequestParam(value = "username", required = true) String username) {
        return username;
    }

访问 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/requestParam?username=tom 得到结果:
tom


2.2 获取全部的请求参数 Map<String, String>

image.png
由源码注释可知,如果方法参数是一个 Map<String, String> 就可以获得全部的请求参数名和值。
示例:(示例中直接返回Map,供SpringBoot自动解析成为JSON,打印在浏览器上)

@RequestMapping(value = "/requestParamMap")
    public Map<String, String> requestParamMap(@RequestParam Map<String, String> requestParam) {
        return requestParam;
    }

访问路径 : http://127.0.0.1:8080/SpringMVCLearning/annotationTest/requestParamMap?username=tom&age=100&address=Shenzhen 得到结果:
名字

3. @PathVariable 路径变量

@PathVariable 源码:

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
    @AliasFor("name")
    String value() default "";

    @AliasFor("value")
    String name() default "";

    boolean required() default true;
}

3.1 获得具体的路径变量

示例:

@RequestMapping(value = "/pathVariable/{id}")
    public String pathVariable(@PathVariable(value = "id") Integer id) {
        return "Input Id is " + id;
    }

访问 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/pathVariable/1 ,得到结果:
Input Id is 1


3.2 获得全部的路径变量 Map<String, String>

image.png
由源码注释可知,如果方法参数是Map<String, String> , 则这个Map将会被填充为 全部的路径变量名和值。

@RequestMapping(value = "/pathVariableMap")
    public String pathVariableMap(@PathVariable Map<String, String> map) {
        map.get("xxx");
        return "success";
    }

4. @RequestHeader 获取请求头属性

@RequestHeader 源码:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader {

	@AliasFor("name")
	String value() default "";
    
	@AliasFor("value")
	String name() default "";
    
	boolean required() default true;
	
	String defaultValue() default ValueConstants.DEFAULT_NONE;

}


4.1 获取具体的请求头属性

示例:

@RequestMapping(value = "/requestHeader")
    public Map<String, Object> requestHeader(@RequestHeader(value = "Connection") String connection,
                                @RequestHeader(value = "Host") String host) {
        Map<String, Object> map = new HashMap<>();
        map.put("Connection", connection);
        map.put("Host", host);
        return map;
    }

访问路径 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/requestHeader ,得到结果:
image.png


4.2 获取全部的请求头属性

image.png
根据源码注释可知,如果方法参数为 Map<String, String> 或者 HttpHeaders 将会被填充全部的请求头名称和值。

@RequestMapping(value = "/requestHeaderMap")
    public Map<String, String> requestHeaderMap(@RequestHeader Map<String, String> map) {
        return map;
    }

访问 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/requestHeaderMap,得到结果:
image.png
HttpHeadersMultiValueMap 的实现类

5. @CookieValue 获得Cookie中的属性

@Cookie 源码:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CookieValue {
	@AliasFor("name")
	String value() default "";

	@AliasFor("value")
	String name() default "";

	boolean required() default true;

	String defaultValue() default ValueConstants.DEFAULT_NONE;
}

5.1 获得具体的Cookie属性

 @RequestMapping(value = "/cookieValue")
    public String cookieValue(@CookieValue("name") String name,
                              @CookieValue("name") HttpCookie cookie) {
        System.out.println(cookie.getName() + cookie.getValue());
        return name;
    }

6. @RequestBody 获得请求体

@RequestBody 源码:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {

	boolean required() default true;
}

6.1 获得请求体内容

@RequestMapping(value = "/requestBody")
    public String requestBody(@RequestBody String requestBody) {
        return requestBody;
    }

7. @RequestAttribute 获取请求域中的值

@RequestAttribute 源码 :

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestAttribute {

	@AliasFor("name")
	String value() default "";

	@AliasFor("value")
	String name() default "";

	boolean required() default true;

}

7.1 获得具体的请求域中的值

取出之前通过 ServletRequest.setAttribute(String name, Object o) 设置的 Attribute
示例:

	@RequestMapping(value = "/forward")
    public String forward(HttpServletRequest request) {
        // 设置Attribute
        request.setAttribute("username", "tom"); 
        return "forward:/annotationTest/requestAttribute";
    }

    @ResponseBody
    @RequestMapping(value = "/requestAttribute")
    public String requestAttribute(@RequestAttribute("username") String username) {
        return username;
    }

访问路径 http://127.0.0.1:8080/SpringMVCLearning/annotationTest/forward ,得到结果:
tom

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值