Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring @RequestHeader Annotation

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 739
    Comment on it

    Spring @RequestHeader Annotation

    Spring MVC provides annotation @RequestHeader that can be used to map controller parameter to request header value. Here is the simple usage of spring @RequestHeader annotation.

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    //..
    
    @Controller
    public class SpringFirstController {
    
        @RequestMapping(value = "/firstPage.htm")
        public String hello(@RequestHeader(value="User-Agent") String userAgent)
    
            //..
        }
    }

    In above code, we defined a controller method hello() which is mapped to URL /firstPage.htm. Also we bind the parameter String userAgent using @RequestHeader annotation. When spring maps the request, it checks http header with name User-Agent and bind its value to String userAgent. If the header value that you specified does not exists in request, Spring will initialize the parameter with null value.

    In case you want to set default value of parameter you can do so using defaultParameter attribute of spring @RequestHeader annotation.

    @RequestMapping(value = "/firstPage.htm")
    public String hello(@RequestHeader(value="User-Agent", defaultValue="foo") String userAgent)
    
        //..
    }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: