user3217883
user3217883

Reputation: 1465

restful post with additional url parameters?

I have a web service which consumes a json request and outputs a json response. I have an issue where the customer needs to send an additional parameter in the url that can't be in the json body. Is there a way to do that?

For example, here is the method of a @WebService that consumes the incoming json request:

    @POST
    @Path("/bsghandles")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public BsgHandleResponse getBsgHandlesJson(BsgHandleRequest obj) {
        HttpServletRequest request = getRequestObject();

        return processGetBsgHandleByRateCode("key", obj.getRateCodes(), obj.getCorp(), 
            obj.getHeadend(), obj.getEquipmentProtocolAiu(), obj.getEquipmentTypeAiu(), request);

    }

Notice that "key" is a hard-coded parameter. I need that parameter to be passed to it by the user in the url, but not the json structure. Is there a way to do that?

Upvotes: 1

Views: 1003

Answers (2)

cassiomolin
cassiomolin

Reputation: 130907

Just add a parameter annotated with @QueryParam to your method:

@POST
@Path("/bsghandles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public BsgHandleResponse getBsgHandlesJson(@QueryParam("key") String key, 
                                           BsgHandleRequest obj) {

    ...
}

And consume it using:

POST /api/bsghandles?key=value HTTP/1.1
Content-Type: application/json
Accept: application/json

{ 
    ...
}

Upvotes: 1

Cesar Villasana
Cesar Villasana

Reputation: 687

Yes there is!

You can pass it as a Query Param. For example:

www.yourhost.com/server?key=value

In java, yo can define it like this in your code:

@GET
@Path("/server")
public Response myMethod(@QueryParam("key") String value) { //your RESTFULL code}

So if you call that url as said before, you will have what you want in the variable value.

Hope it helps..

Cheers

Upvotes: 1

Related Questions