使用PathVariable对类进行RequestMapping

时间:2014-03-04 14:47:43

标签: rest spring-mvc controller interceptor restful-architecture

我正在开发spring mvc中的restful webservices。来自客户端的所有请求将在请求映射中包含/sessions/{token} uri前缀。例如:

@Controller
@RequestMapping(CardsController.URL_PREFIX)
public class CardsController{
    protected static final String URL_PREFIX = "/sessions/{token}/cards";
    @RequestMapping(method=RequestMethod.GET, value = "/get") 
    public @ResponseBody BaseResponse getCardList(){...}
    ...
}

@Controller
@RequestMapping(AccountsController.URL_PREFIX)
public class AccountsController{
    protected static final String URL_PREFIX = "/sessions/{token}/accounts";
    @RequestMapping(method=RequestMethod.GET, value = "/get") 
    public @ResponseBody BaseResponse getAccountsList(){...}
    ...
}

@Controller
@RequestMapping(BillsController.URL_PREFIX)
public class BillsController{
    protected static final String URL_PREFIX = "/sessions/{token}/bills";
    @RequestMapping(method=RequestMethod.GET, value = "/get") 
    public @ResponseBody BaseResponse getBillsList(){...}
    ...
}

我可以制作一个基本控制器,所有这些控制器都可以扩展这个基本控制器。但是我如何在每个请求中获得{token} PathVariable值并对用户进行身份验证。如果我在请求头中发送此令牌值,我可以在拦截器中获取它并在控制器之前验证用户。但是我知道当我把这个令牌信息放在uri中时,它必须在每个函数体中进行身份验证,这是不好的。是否存在除拦截器之外的预控制器机制,可以处理uri并完成某项工作。

提前致谢。

0 个答案:

没有答案
相关问题