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サーブレットに到達したかどうかはわかりません...

適切に処理できるように、で始まるものをワイルドカード化するにはどうすればよいですか?/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 に答える