我觉得这是一个常见问题,但我研究过的都还没有起作用......
在我的 web.xml 中,我有所有 REST 调用的映射 -
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
如果 URL 是 - 则此方法效果很好
GET /rest/people
but fails如果是
GET /rest/people/1
I get a 400 Bad Request
错误说The request sent by the client was syntactically incorrect ()
。我不确定它是否已经到达 Spring servlet 并被路由......
我怎样才能使用通配符anything开始于/rest
以便能够妥善处理?
换句话说,我希望以下所有内容均有效 -
GET /rest/people
GET /rest/people/1
GET /rest/people/1/phones
GET /rest/people/1/phones/23
Edit- 根据要求的控制器代码
@Controller
@RequestMapping("/people")
public class PeopleController {
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody String getPeople() {
return GsonFactory.getInstance().toJson(LookupDao.getInstance().getPeople());
}
@RequestMapping(value="{id}", method=RequestMethod.GET)
public @ResponseBody String getPerson(@PathVariable String id) {
return GsonFactory.getInstance().toJson(LookupDao.getInstance().getPerson(id));
}
}
Answer
@matsev 如果我有的话似乎并不重要/
有或没有。
当我转换变量名称以供公众查看时,我更改了一些内容以使其正常工作。
Original
@RequestMapping(value="{id}", method=RequestMethod.GET)
public @ResponseBody String getPerson(@PathVariable String userId) {
return GsonFactory.getInstance().toJson(LookupDao.getInstance().getPerson(userId));
}
我发布的内容
@RequestMapping(value="{id}", method=RequestMethod.GET)
public @ResponseBody String getPerson(@PathVariable String id) {
return GsonFactory.getInstance().toJson(LookupDao.getInstance().getPerson(id));
}
变量名称不匹配让我陷入...我将其留在这里作为对所有人的警告...匹配您的变量名称!