SpringMVC常用注解

@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";
}

使用场景
传递的参数必须和RequestParam的value一致,但不存在这个参数的时候,默认会报错,如果这个参数并非必须,那么可以将@RequestParam的required设置false,age的类型最好为Integer,这样当age参数不存在的时候,会保存,因为不能将null赋给int,如果非要用int,那么可以加defaultValue属性给个默认值

public String testRequestParam(@RequestParam(value = "username") String un,
                               @RequestParam(value = "age",required=false,defaultValue = "0") int age)

@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 "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

使用

    @RequestMapping(value = "/testRequestHeader")
    public String requestHeader(@RequestHeader(value = "Accept-Language") String header ,String name){
        System.out.println("header: "+header);
        System.out.println("name "+name);
        return "info";

    }

@CookieValue

@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 "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

使用

   @RequestMapping(value = "/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String sessionId){
        System.out.println("sessionId: "+sessionId);
        return "info";

    }

你可能感兴趣的:(SpringMVC)