How to pass two request parameters in url
In this example, the service URL is of 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 to pass two (or more) request 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();
}
source to share
/buy?projectionId=value1&place=value2
Have a look at https://en.wikipedia.org/wiki/Query_string for more information. And since this is an HTTP PUT, you cannot just open that URL in your browser, you can write a simple REST client or use a browser extension like Postman in Chrome.
source to share
The request parameter is the thing after ?
in the URI, while the path parameter is the parameter before ?
the URI.
If you need two inputs for your method, you can go with any combination of query parameters and paramart => four combinations
A good convention is that the path parameters should denote some sort of resource identifier because it is part of its address, whereas the request parameters have a more specific form / shape / filtering response.
In your case, I would encode both parameters 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 will look like this:
${host}/buy/1337/place/42
source to share
Thanks for your input guys, I fixed this.
It looks like I had to add the path parameter to the extra parameters and pass the extra parameters as requested, not the path parameter. The code looks like 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();
});
});
Thank you.
source to share