12

In this example, the URL for a service has the form /projection/projectionId:

@Stateless
@Path("projection")
public class ProjectionManager {

@Inject
private ProjectionDAO projectionDAO;

@Inject
private UserContext userContext;

@GET
@Path("{projectionId}")
@Produces("application/json")
public String places(@PathParam("projectionId") String projectionId) {
    return projectionDAO.findById(Long.parseLong(projectionId)).getPlaces().toString();
}}

How can I pass two (or more) query parameters to access the service using this code:

@PUT
@Path("/buy")
public Response buyTicket(@QueryParam("projectionId") String projectionId, @QueryParam("place") String place) {
    Projection projection = projectionDAO.findById(Long.parseLong(projectionId));
    if(projection != null) {
        projectionDAO.buyTicket(projection, userContext.getCurrentUser(), Integer.parseInt(place));
    }

    return Response.noContent().build();
}
1
  • 1
    You just invoke HTTP PUT to /buy?projectionId=1. Commented Jun 17, 2015 at 12:06

3 Answers 3

15
/buy?projectionId=value1&place=value2

Take a look at https://en.wikipedia.org/wiki/Query_string for further information. And since it is HTTP PUT you cannot simply open that URL in your browser, you can write some simple REST client or use browser extension like Postman in Chrome.

Sign up to request clarification or add additional context in comments.

Comments

0

Query parameter is the thing after the ? in the URI, while path parameter is the parametrer before the ? in the URI.

If you need two inputs to your method, you can go with any combination of query param and path param => four combinations

It's a good convention that path params should denote some kind of identity of the resource, because it's part of it's address, while query params more some form/shape/filtering of the response.

In your case, I'd encode both params as path parameters, so the code would look like this:

@PUT
@Path("/buy/{projectionId}/place/{place}")
public Response buyTicket(@PathParam("projectionId") String projectionId, @PathParam("place") String place){
    Projection projection = projectionDAO.findById(Long.parseLong(projectionId));
    if(projection != null){
        projectionDAO.buyTicket(projection, userContext.getCurrentUser(), Integer.parseInt(place));
    }

    return Response.noContent().build();
}

The URL would look like: ${host}/buy/1337/place/42

Comments

0

Thanks for your input guys, I have fixed it.

It looks like I had to add the path parameter to the additional parameters, and pass additional parameters on the request, instead of the path parameter. Code looks as below,

it('should get a customer, searches with a customer name', (done) => {
var pathParams = {};
var body = {};
var additionalParams = {
queryParams: {
name: 'Ominathi'
}
};
//apigClient.invokeApi(pathParams, '/customer', 'GET', queryParams, body)
apigClient.invokeApi(pathParams, '/customer', 'GET', additionalParams, body)
.then(response => {
expect(response.status).toBe(200);
done();
})
.catch(err => {
fail(err);
done();
});
});

Thanks.

Ref: https://www.npmjs.com/package/aws-api-gateway-client

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.