0

I'm trying to create a GET request, where I have two different requests. Since both resources are related with each other, I'm trying to put them under one 'sub-resource'. The first one is:

  @QueryParam("{customerId}")
  public List<customerModel> getCustomer(@QueryParam("customerId") Long customerId) {....}

this fetches the customer's name depending on the customerId

@QueryParam("{customerId}/details")
public List<customerDetailModel> getCustomerDetail(@QueryParam("customerId") Long customerId) {....}

this fetches the detailed information of the customer (phone number, address, etc.)

I'm running the first one with the following (works fine) : ......?customerId=7453112

but I can't reach the second request when I'm hitting the following URL: ......?customerId=7453112/details

Any suggestions?

Aiguo
  • 3,416
  • 7
  • 27
  • 52

3 Answers3

0

You need to go with @RequestMapping.

Something like: @RequestMapping("/{customerId}") and @RequestMapping("/{customerId}/details").

Your URL becomes 7453112/details instead of query params.

RP-
  • 5,827
  • 2
  • 27
  • 46
0

You can specify the /details in an @Path annotation, and then use the same query parameter, like this:

@Path("/details/{customerId}")
public List<customerDetailModel> getCustomerDetail(@PathParam("customerId") Long customerId) {....}

Then your URL would look like this:

.../details/7453112

Or if you want to keep using it as a query parameter you can do something like this:

@Path("/details")
public List<customerDetailModel> getCustomerDetail(@QueryParam("customerId") Long customerId) {....}

using this url:

.../details?customerId=xxx

Alex
  • 827
  • 8
  • 18
  • In this particular case, what should be the 'best practice'? I mean should it be just PathParam or should it be QueryParam? – Aiguo Jan 20 '17 at 21:15
  • I think because the customer ID is the subject of the method's functionality, it makes sense to use it as a query param. There is a good question about that here: http://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam – Alex Jan 20 '17 at 21:27
0

Resource methods must be annotated with @Path and with a request method designator such as @GET, @POST, @PUT, @DELETE, @HEAD or @OPTIONS. Your @QueryParam annotation is misplaced.

By the way, for this situation, you should consider using a path parameter instead of a query parameter. Hence you should use @PathParam instead of @QueryParam in the method parameter. For more details on when to use query or path parameters, have a look at this answer

Also, not sure why you are returning a List if you are requesting a resource by its unique identifier. In this situation, you are supposed to return a representation of a single resource (and not a represention of multiple resources).

So your resource class would be as following:

@Path("customers")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class CustomerResource {

    @GET
    @Path("{customerId}")
    public CustomerModel getCustomer(@PathParam("customerId") Long id) {
        ...
    }

    @GET
    @Path("{customerId}/details")
    public CustomerDetailModel getCustomerDetail(@PathParam("customerId") Long id) {
        ...
    }
}

The first endpoint can be requested as following:

GET http://example.com/api/customers/1

And the second endpoint can be requested as following:

GET http://example.com/api/customers/1/details
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359