How can I map semicolon-separated PathParams in Jersey?

后端 未结 1 812
孤独总比滥情好
孤独总比滥情好 2020-12-29 09:21

Is there a way to use this parameter style:

/products/123;456;789

in JAX-RS with Jersey? If I use PathParam, only the first para

相关标签:
1条回答
  • 2020-12-29 10:02

    When you use semicolon, you create Matrix parameters. You can use either @MatrixParam or PathSegment to get them. Example:

     public String get(@PathParam("param") PathSegment pathSegment)
    

    Pay attention that Matrix parameters are these that follow the original parameter. So in case of "123;456;789" - 123 is path parameter, while 456 and 789 are the names of matrix parameters.

    So if you want to get products by ids, you can do something like this:

    public List<Product> getClichedMessage(@PathParam("ids") PathSegment pathSegment) {
        Set<String> ids = pathSegment.getMatrixParameters().keySet();
        // continue coding
    }
    

    Pay attention that your url should be /products/ids;123;456;789

    Actually, IMO it is not a very good design: you use matrix parameter name as a value. I think using query parameters is better: /products?id=123&id=456&id=789, so you can easily get them in method:

    public List<Product> getClichedMessage(@QueryParam("id") List<String> ids)
    
    0 讨论(0)
提交回复
热议问题