2

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

在我的 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
@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我是否在那里似乎并不重要/

当我将变量名转置以供公众查看时,我更改了几件事以使其正常工作。

原来的

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

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

4

1 回答 1

4

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

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

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

于 2012-04-03T19:44:28.383 回答