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

    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值