在将其标记为重复之前,只想让你们知道我已经检查了此处发布的问题: What is the difference between @PathParam and @PathVariable
事实是,如果PathParam和PathVariable的用法相同(只有一个来自JAX-RS API,一个由Spring提供),为什么使用一个给我null而另一个给我正确的原因呢?值?
我正在使用Postman来按以下方式调用服务: 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
}
}
答案 0 :(得分:0)
我认为您项目中的pathparam在javax.ws下...这不是他们所说的。它在websocket中使用,这意味着它不是http注释。 JBoss实现pathparam需要额外的jar。