【问题标题】:Filtering a resource in Jersey similar to @RequestMapping "Params" attribute of Spring在 Jersey 中过滤类似于 Spring 的 @RequestMapping "Params" 属性的资源
【发布时间】:2016-02-26 10:50:01
【问题描述】:

我正在将我所有的 Spring 服务转换为 Jersey 时,遇到了一个关于如何将 Spring 的 RequestParam 的 params 功能转换为 Jersey 的问题?

@RequestMapping(value = "/earnings", params = "type=csv")

春天:

@RequestMapping(value = "/earnings", params = "type=csv")
public void earningsCSV() {}

@RequestMapping(value = "/earnings", params = "type=excel")
public void earningsExcel() {}

@RequestMapping("/earnings")
public void earningsSimple() {}

球衣:

@Path("/earnings") 
public void earningsCSV() {}

@Path("/earnings")
public void earningsExcel() {}

@RequestMapping("/earnings")
public void earningsSimple() {}

如何在 Jersey 中指定类型“csv/excel”? Jersey 是否支持基于参数的过滤请求?

如果没有,有什么方法可以实现吗? 我正在考虑一个过滤器来处理它们并重定向请求,但我有近 70 多个服务需要以这种方式解决。 所以我最终必须为所有这些都写一个过滤器。此外,这听起来不像是一种干净的方法。

任何建议将不胜感激。 提前致谢。

【问题讨论】:

    标签: spring spring-mvc jersey jersey-2.0 request-mapping


    【解决方案1】:

    Jersey 中没有配置来定义这一点,就像它在春天所做的那样。

    我通过创建一个父服务来解决这个问题,该服务接受调用并根据参数将调用重定向到相应的服务。

    @Path("/earnings")
    public void earningsParent(@QueryParam("type") final String type) {
        if("csv".equals(type)) 
             return earningsCSV();
        else if("excel".equals(type)) 
             return earningsExcel();
        else 
             return earningsSimple();
    }
    
    public void earningsCSV() {}
    
    public void earningsExcel() {}
    
    public void earningsSimple() {}
    

    我觉得这种方法比过滤器更好,因为它不需要开发人员在将来需要扩展过滤器时去更改它。

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      相关资源
      最近更新 更多