0

Before marking this as duplicate, just wanted you guys to know I have checked out the question posted here: What is the difference between @PathParam and @PathVariable

Thing is, if the usage of PathParam and PathVariable are same (only that one is from the JAX-RS API and one is provided by Spring), why is it that using one gives me null and the other gives me the proper value?

I am using Postman to invoke the service as: http://localhost:8080/topic/2

(I'm very new to SpringBoot)

Using PathParam :

import javax.websocket.server.PathParam;
import org.apache.tomcat.util.json.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping(method=RequestMethod.GET,path="/topic/{id}")
    public Topic getById(@PathParam("id") long id) throws ParseException {
        return topicService.getTopicById(id);  //-- here id comes as null (when id is declared as a wrapper type - Long, else it throws an error)
    }
}

Using PathVariable:

@RestController
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping(method=RequestMethod.GET,path="/topic/{id}")
    public Topic getById(@PathVariable("id") long id) throws ParseException {
        return topicService.getTopicById(id);  //-- here id comes as 2
    }
}
Soumav
  • 385
  • 1
  • 6
  • 25

1 Answers1

0

I think the pathparam in your project is under javax.ws... This one is not what they talked about. It is used in websocket, which means it is not a http annotaiton. JBoss implement pathparam needs additional jars.

wl.GIG
  • 306
  • 1
  • 2
  • 13