【问题标题】:Multiple GET operations in REST differentiated by queryparam and pathparam is possible?REST 中由 queryparam 和 pathparam 区分的多个 GET 操作是可能的吗?
【发布时间】:2016-06-27 02:42:17
【问题描述】:

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

@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 方法是否有效?任何意见或意见表示赞赏。

谢谢。

【问题讨论】:

  • 您是否在客户端请求中设置了Accept: application/json 标头?你的methin定义没有错
  • 抱歉回复晚了。我确实在标题中有它。有什么想法吗?
  • 您的代码看起来不错,拥有 2 个 GET 是有效的。问题可能出在您用于 JSON 的内容上吗?为了查看是否是问题所在,请尝试返回字符串而不是 SearchResult/SuccessResponse。
  • 谢谢大家。我通过在代码中添加更多注释解决了这个问题。我在下面分享了更新的代码。

标签: jax-rs jersey-2.0


【解决方案1】:

//在两个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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多