Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the request param values in spring using HTTPServletRequest

I want to read the requestParams data from the url using HttpServletRequest

http://localhost:8080/api/type?name=xyz&age=20

The method in my controller will not have @RequestParam defined,it will be just

@RequestMapping(value = "/**", method = RequestMethod.GET)
    public ResponseEntity<String> getResponse(
            final HttpServletRequest request) {}

I want to read using request only the params not the entire url.

like image 842
user1195292 Avatar asked Feb 24 '12 09:02

user1195292


3 Answers

first, why you are define:

@RequestMapping(value = "/**", method = RequestMethod.GET)`

?

maybe you should use:

@RequestMapping(value = "/api/type", method = RequestMethod.GET)

and read param :

request.getParameter("name");
request.getParameter("age"):
like image 86
Bill Xie Avatar answered Sep 20 '22 03:09

Bill Xie


xiang is right for your exact question: "I want to read using request only the params"

But why do you want to make it so difficult. Spring supports you, so you do not need to handle the request object by yourself for such common tasks:

I recommend to use

@RequestMapping(value = "/*", method = RequestMethod.GET)
public ResponseEntity<String> getResponse(
    @RequestParam("name") String name
    @RequestParam("age") int age){

    ...
}

instead.

@See Spring Reference Chapter 15.3.2.4. Binding request parameters to method parameters with @RequestParam

like image 28
Ralph Avatar answered Sep 23 '22 03:09

Ralph


You can use

request.getParameter("parameter name") 
like image 22
Rocky Avatar answered Sep 22 '22 03:09

Rocky