gpt4 book ai didi

java - RESTful servlet URLs - web.xml 中的 servlet 映射

转载 作者:行者123 更新时间:2023-11-30 07:27:03 25 4
gpt4 key购买 nike

我觉得这是一个普遍的问题,但我研究过的任何东西都没有奏效......

在我的 web.xml 中,我有一个所有 REST 调用的映射 -

  <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

如果 URL 是 -

GET /rest/people

失败如果是

GET /rest/people/1

我收到一个 400 Bad Request 错误,提示 The request sent by the client was syntactically incorrect ()。我不确定它是否已到达 Spring servlet 以进行路由...

我如何通配以 /rest 开头的任何内容,以便对其进行适当处理?

换句话说,我希望以下所有内容都有效 -

GET /rest/people
GET /rest/people/1
GET /rest/people/1/phones
GET /rest/people/1/phones/23

编辑 - 请求的 Controller 代码

@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));
}
}

回答

@matsev 是否​​有 / 似乎并不重要。

当我为公众 View 调换变量名称时,我更改了一些内容以使其正常工作。

原创

@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));
}

变量名不匹配让我...我把它留在这里作为对所有人的警告...匹配您的变量名!

最佳答案

尝试在 {id} 之前添加一个 /:

@RequestMapping(value="/{id}", method=RequestMethod.GET)

如果没有它,id 将直接附加到人员 url,例如 /rest/people1 而不是 /rest/people/1

关于java - RESTful servlet URLs - web.xml 中的 servlet 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10000008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com