Search code examples
springspring-mvcjerseyjersey-2.0request-mapping

Filtering a resource in Jersey similar to @RequestMapping "Params" attribute of Spring


I'm in the process of converting all my Spring Services to Jersey, when I came across an issue about how to convert the RequestParam's params feature of Spring to Jersey?

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

Spring:

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

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

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

Jersey:

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

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

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

How to specify the type "csv/excel" in Jersey? Does Jersey even support Filtering Requests based on Param's?

If not, is there any way I can achieve this? I was thinking of a filter which process them and re-directs requests, but I've nearly 70+ services which needs to be addressed this way. So I'll have to end up writing a filter for all of them. Also, It doesn't sound like a clean approach.

Any suggestions would be appreciated. Thanks in advance.


Solution

  • There was no configuration in Jersey to define this, the way its done in spring.

    I solved this by creating a Parent Service which accepts the call and re-directs the call to respective services based on param.

    @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() {}
    

    I felt this approach was better than Filter as it doesn't need the developer to go change the Filter in future if it needs to be extended.