本文介绍了Spring请求针对特定路径变量值映射到其他方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@Controller
@RequestMapping("/authors")
public class AuthorController {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Author getAuthor(
final HttpServletRequest request,
final HttpServletResponse response,
@PathVariable final String id)
{
// Returns a single Author by id
return null;
}
@RequestMapping(value = "/{id}/author-properties", method = RequestMethod.GET)
public AuthorProperties getAuthorProperties(
final HttpServletRequest request,
final HttpServletResponse response,
@PathVariable final String id)
{
// Returns a single Author's List of properties
return null;
}
@RequestMapping // How to map /authors/*/author-properties to this method ????
public List<AuthorProperties> listAuthorProperties(
final HttpServletRequest request,
final HttpServletResponse response)
{
// Returns a single Author's List of properties
return null;
}
}
class Author {
String propertiesUri;
// other fields
}
class AuthorProperties {
String authorUri;
// other fields
}
基本上我需要:
- / authors-列出所有作者
- / authors / 123-通过id 123获取作者
- / authors / 123 / author-properties-获取123位作者的AuthorProperties对象
- / authors / * / author-properties-提取所有AuthorProperties的列表作者
- /authors - listing all the authors
- /authors/123 - fetching author by id 123
- /authors/123/author-properties - fetching the AuthorProperties object for a 123 author
- /authors/*/author-properties - fetching List of AuthorProperties for all authors
当我尝试
@RequestMapping(value = "/*/author-properties", method = RequestMethod.GET)
仍将 / authors / * / author-properties
映射到路径变量值为 *的getAuthorProperties方法。
It was still mapping /authors/*/author-properties
to getAuthorProperties method with path variable value as "*".
推荐答案
如果获取所有作者的AuthorProperty列表是很常见的情况,那么我认为您应该创建一个URI: / author-properties。否则,Bohuslav给出的答案就是您想要的。
If fetching List of AuthorProperties for all authors is common case, then I think that you should make an URI like this: "/author-properties". Otherwise answer given by Bohuslav is what you want.
这篇关于Spring请求针对特定路径变量值映射到其他方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!