Прежде чем пометить это как дубликат, я просто хотел, чтобы вы, ребята, знали, что я проверил вопрос, размещенный здесь: В чем разница между @PathParam и @ PathVariable
Дело в том, еслииспользование PathParam и PathVariable одинаковы (только то, что одно из API JAX-RS, а другое предоставляется Spring), почему использование одного дает мне ноль, а другое дает мне правильное значение?
Я использую Почтальон для вызова службы как: http://localhost:8080/topic/2
(я очень новичок в SpringBoot)
Использование 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)
}
}
Использование 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
}
}