25

I am using spring-boot 1.4.3.RELEASE for creating web services, whereas, while giving the request with http://localhost:7211/person/get/ram, I am getting null for the id property

@RequestMapping(value="/person/get/{id}", method=RequestMethod.GET, produces="application/json")
    public @ResponseBody Person getPersonById(@PathParam("id") String id) {
        return personService.getPersonById(id);
    }

Can you please suggest me, is there anything I missed.

Ram Kowsu
  • 711
  • 2
  • 10
  • 30
  • "@PathParam" is JAX-RS annotation and used in implementation like Jersey. Spring equivalent annotation is "@PathVariable". – Dexter Jan 19 '20 at 08:40

4 Answers4

64

The annotation to get path variable is @PathVariable. It looks like you have used @PathParam instead which is incorrect.

Check this out for more details:

requestparam-vs-pathvariable

Community
  • 1
  • 1
DeepakV
  • 2,420
  • 1
  • 16
  • 20
13

As above answers already mentioned @PathVariable should be used, I thought to clear the confusion between @PathVariable & @PathParam.

Most people get confused on this part because Spring and other rest implementation like Jersey use sightly different annotations for the same thing.

@QueryParam in Jersey is @RequestParam in Spring Rest API.

@PathParam in Jersey is @PathVariable in Spring Rest API.

Dexter
  • 4,036
  • 3
  • 47
  • 55
3

Use @PathVariable annotation instead of @PathParam.

1

id should be Long instead of String at @PathVariable. If so, then ...

@RequestMapping(value="/person/get/{id}", method=RequestMethod.GET, produces="application/json")
public @ResponseBody Person getPersonById(@PathVariable("id") Long id) {
    return personService.getPersonById(id);
}
testuser
  • 793
  • 3
  • 13
  • 35