【问题标题】:Spring @PathVariable create custom objectSpring @PathVariable 创建自定义对象
【发布时间】:2013-06-26 12:27:28
【问题描述】:

我的应用程序中有一个实体,它有一个组合的ID
在我的控制器中,我以这种方式使用@PathVariable 获取此实体:

@RequestMapping("/{year}/{code}")
public MyCustomObj get(@PathVariable Integer year, @PathVariable Integer code){
    return myCustomObjRepository.findOne(new CustomId(year, code));
}

是否有可能,使用像WebArgumentResolver 这样的组件,让我的方法以这种方式工作:

@RequestMapping("/{customObj}")
public MyCustomObj get(@PathVariable CustomId id){
    return myCustomObjRepository.findOne(id);
}

有一个类似的 URL:/application/2013/06

【问题讨论】:

标签: spring http-request-parameters


【解决方案1】:

您可以通过注册自定义过滤器并覆盖 doFilterMethod 来做到这一点,如下所示。

 public void doFilter(ServletRequest arg0, ServletResponse arg1,
        FilterChain chain){
 HttpServletRequest request = (HttpServletRequest) arg0;
 HttpServletResponse response = (HttpServletResponse) arg1;
 String args = request.getRequestURI();
 //get your year and code
 MyCustomObj obj = new CustomId(year, code);
 response.sendRedirect("someURL/obj");
 }

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 2014-12-14
    • 2010-09-18
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2015-05-15
    • 2016-12-15
    • 2012-07-03
    相关资源
    最近更新 更多