REST中的多个GET操作可以通过queryparam和pathparam区分?

时间:2016-03-11 18:19:50

标签: jax-rs jersey-2.0

我正在尝试使用Jersey 2.x / Java和两种不同的GET方法创建基于REST的服务。 它们都需要具有相同的端点,并且一个操作是搜索产品列表,这将获取查询参数列表。另一个操作是将产品手册下载为pdf文件,这只需要一个路径参数。我的资源类如下所示:

@Path("/domain")

public class MyResource  {


    @GET
    @Path("/home/products") 
    @Consumes({ MediaType.APPLICATION_JSON })
    @Produces({MediaType.APPLICATION_JSON })

    public SearchResult loansHomeLoansDocumentsGet(
        @QueryParam("productType") String productType,
        @QueryParam("productSubType") String productSubType,
        @QueryParam("productSource"
        @QueryParam("toDate") String toDate) throws Exception {

        .......
    }


    @GET
    @Path("/home/products/{productId}") 
    @Consumes({ MediaType.APPLICATION_JSON })
    @Produces({MediaType.APPLICATION_JSON})

    public SuccessResponse loansHomeLoansDocumentsDocumentReferenceIdGet(@PathParam("productId") String productId) {
           .......
    }


......
}

但是当我试着尝试时,它会抛出406不可接受的异常。

javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
at org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.getMethodRouter(MethodSelectingRouter.java:529) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.access$000(MethodSelectingRouter.java:94) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.MethodSelectingRouter$4.apply(MethodSelectingRouter.java:779) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.MethodSelectingRouter.apply(MethodSelectingRouter.java:371) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61) ~[jersey-server-2.22.1.jar:?]
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) ~[jersey-common-2.22.1.jar:?]
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318) [jersey-server-2.22.1.jar:?]

以这种方式保留两个GET方法是否有效?任何评论或意见都表示赞赏。

感谢。

1 个答案:

答案 0 :(得分:0)

//在两个get方法中添加了@BeanParam EntityRequest。这对我有用!

@GET
@JSONP(queryParam = JSONP.DEFAULT_QUERY)
@ApiOperation(value = "Get a list of Products", notes = "Search for products.", position = 1, response = ProductEntityCollectionResponse.class)
@ApiResponses(value = {@ApiResponse(code = HttpURLConnection.HTTP_OK, message = HTTPStatusMessageConstants.SUCCESS_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = HTTPStatusMessageConstants.INTERNAL_SERVER_ERROR_MESSAGE)})
public ProductEntityCollectionResponse getMany(@BeanParam EntityCollectionRequest request, @BeanParam ProductSearch search) {

    List<Product> pojos = myService.findProducts(request, search);
    ProductEntityCollectionResponse response = new ProductEntityCollectionResponse(request, pojos);
    return response;
}

@GET
@Path("/{id}")
@JSONP(queryParam = JSONP.DEFAULT_QUERY)
@ApiOperation(value = "Get a single product", notes = "Search for a specific product with the provided ID.", response = Product.class)
@ApiResponses({@ApiResponse(code = HttpURLConnection.HTTP_OK, message = HTTPStatusMessageConstants.SUCCESS_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = HTTPStatusMessageConstants.INTERNAL_SERVER_ERROR_MESSAGE)})
public Product getOne(
        @BeanParam EntityRequest entityRequest,
        @ApiParam(value = "The ID of the product to retrieve", name = "id", required = true, allowMultiple = false) @PathParam("id") String id) {
    return myService.findProduct(id);
}