Search code examples
javajsonweb-servicesjax-rsenunciate

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?


Solution

  • 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
    
    { 
        ...
    }