users@jersey.java.net

[Jersey] Re: Retrieving HttpRequestContext in Service Request

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Wed, 11 Apr 2012 08:54:18 -0400

Not sure of the details of your post but here is something from the
archives that may help:

http://markmail.org/message/ggrailrz67ugpzgv

On 04/10/2012 12:55 PM, sdoca sdoca wrote:
> 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!


-- 
Regards,
Farrukh Najmi
Web: http://www.wellfleetsoftware.com