users@jersey.java.net

[Jersey] Retrieving HttpRequestContext in Service Request

From: sdoca sdoca <sdoca_at_shaw.ca>
Date: Tue, 10 Apr 2012 10:55:39 -0600

Hi,

I am looking for a way to pass a transaction ID to a web service call to be used for logging/tracking purposes (see http://stackoverflow.com/questions/9915361/how-to-follow-individual-transaction-in-logs-java-ee for more background).

I found the ability to add/retrieve HTTP headers in Jersey using HttpRequestContext.  I modified my resource to get the HttpRequestContext in the method signature and then to retrieve the transaction ID from the header and place that into the slf4j MDC for use in logging:

    @POST
    @Consumes(MediaType.APPLICATION_XML)
    @Path("/subscriber")
    public void createSubscriberAccount(final SubscriberAccount account,
            @Context HttpRequestContext requestContext)
    {
        putTransactionIdInMDC(requestContext);

        try
        {
            provisionService.createSubscriberAccount(account);
        }
        catch (Exception ex)
        {
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    }

    private void putTransactionIdInMDC(HttpRequestContext requestContext)
    {
        String transactionID = requestContext.getHeaderValue(Constants.MDC_KEY_TRANSACTION_ID);

        if (transactionID != null && transactionID.isEmpty())
        {
            String[] strings = transactionID.split(",");
            MDC.put(Constants.MDC_KEY_TRANSACTION_ID, strings[0]);
        }
    }

However, adding the HttpRequestContext parameter to my method signature results in this deploy error:

SEVERE: Method, public void my.resource.ProvisionResource.createSubscriberAccount(my.SubscriberAccount, com.sun.jersey.api.core.HttpRequestContext), annotated with POST of resource, class my.resource.ProvisionResource, is not recognized as valid resource method.

Is there  a different way for me to get the HttpRequestContext from the service request?

Thanks!