2

enter image description hereAm developing an application using spring boot. In Resource am using path variable(**@PathVariable** annotation) and Request Param(**@RequestParam("name")** annotation). My code fetching the request param value but not the path variable value,I am getting the path variable value as null. Please any one suggest me to solve this issue.

@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)  
public void get(@RequestParam("name")  String name, @PathVariable Integer id); {

        System.out.println("name="+name);
        System.out.println("id="+id)
}

URL:
http://localhost:8080/api/user/2?name=neeru

OUTPUT:
name=neeru
id=null

i also tried

**@RequestMapping(value = "/api/user/id={id}", method = RequestMethod.GET)** 

URL:
http://localhost:8080/api/user/id=2?name=neeru but getting same id value=null

i have added one more method -only has @PathVariable

@RequestMapping(path="/api/user/name/{name}", method = RequestMethod.GET)    
   void get( @PathVariable("value=name") String name){

   System.out.println("name="+name)
}

but result is same path variable value name=null

7
  • 1
    could you try api/user/{id} instead of api/user/id={id}? Commented Oct 25, 2017 at 9:14
  • yes,i tried api/user/id={id}?name=abc but getting same null value for id Commented Oct 25, 2017 at 9:17
  • Set a name for the pathvariable like @PathVariable(name="id") so it can get resolved.. Commented Oct 25, 2017 at 9:33
  • yes tried @PathVariable(name="id") and also @PathVariable(value="id") ,both are not working Commented Oct 25, 2017 at 9:38
  • You have a typo... should be @PathVariable(value="name") and not @PathVariable("value=name") Commented Oct 25, 2017 at 10:09

2 Answers 2

6
  1. @PathVariable is to obtain some placeholder from the uri
  2. @RequestParam is to obtain an parameter

Change your endpoint like this

http://localhost:8080/api/user/2/users?name=neeru

`

@RequestMapping(value = "api/user/{id}/users", method = RequestMethod.GET)  
public void get(@RequestParam("name")  String name, @PathVariable("id") Integer id); {
        System.out.println("name="+name);
        System.out.println("id="+id)
}

`

Output:

name = neeru

id = 2

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

not working tried,@RequestMapping(path="api/user/{id}/users", method = RequestMethod.GET) void get(@RequestParam("name") String name, @PathVariable("id") Integer id); and url : localhost:8080/api/user/id/2/users?name=neeru
may be,sorry but not working for me. here is also a ex stackoverflow.com/questions/26794198/… . i have done same ,but dont know why its not working
see my question ,i have attached screenshot,tell me where m doing mistake
thanks,actually i had UserResource interface and UserResourceImpl class ,In spring boot PathVariable annotation is not inherited from interface, i was using @PathVariable at interface thats why is was not working but now m using PathVariable annotation at UserResouceImpl as well,now its working for me.
5

@PathVariable is used for extracting variables in the path like MystyxMac suggested. If you want to extract query parameters then you must use @RequestParam

But your example is a mix of path and query parameter.

You cannot use = in an URL because this is a reserved character: https://www.w3.org/Addressing/URL/url-spec.txt

So either use

/api/user/{id} with @PathVariable 

or

/api/user?id={id} with @RequestParam

3 Comments

m trying /api/user/{id} with @PathVariable but getting null value for id,dont know why i am getting path variable value as null
im trying to write API test cases for scenarios like this. if the endpoint is called as /api/user/null should it give a client error (4xx) or a server error (5xx)?
Always a 4xx becuase 5xx is server error that indicates that your application has an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.