-2

I am trying to create REST based service using Jersey 2.x/Java with two different GET methods. They both need to have same endpoints and one operation is to search a list of products and this takes a list of query parameters. Another operation is to download the product brochure as a pdf file and this takes only a path param. MY My resource class looks like below:

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


......
}

But when I run tried that, it throws 406 not acceptable exception.

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:?]

Is that valid to keep two GET methods in this way ? Any comment or input is appreciated.

Thanks.

bkrish
  • 627
  • 1
  • 11
  • 23
  • 1
    Did you set the `Accept: application/json` header on the client request? There is nothing wrong with your methin definitions – Paul Samsotha Mar 12 '16 at 01:32
  • Apologies for late reply. I do have it in header. Any thoughts ? – bkrish Mar 15 '16 at 02:02
  • Your code looks fine and it is valid to have 2 GETs. Could the issue be with what you're using for JSON? For the sake of seeing if that's the problem, try returning a String instead of SearchResult/SuccessResponse. – Amber Mar 15 '16 at 18:13
  • see http://stackoverflow.com/questions/14251851/what-is-406-not-acceptable-response-in-http – Meiko Rachimow Mar 27 '16 at 20:23
  • Thank you all. I have solved the problem by adding few more annotations in my code. I shared the updated code below. – bkrish Mar 31 '16 at 13:06

1 Answers1

0

//Added @BeanParam EntityRequest in both get methods. This is working for me!

@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);
}
bkrish
  • 627
  • 1
  • 11
  • 23