41

I am using jersey for a REST WS. How do I enable jersey logs at server side?

Long story: I get a clientside exception - but I don't see anything in tomcat logs [It doesn't even reach my method]. Since the stack trace is saying "toReturnValue" it did get something from server. But I don't know what the server said.

Exception in thread "main" java.lang.IllegalArgumentException: source parameter must not be null
 at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:98)
        at com.sun.xml.internal.ws.message.AbstractMessageImpl.readPayloadAsJAXB(AbstractMessageImpl.java:100)
        **at com.sun.xml.internal.ws.client.dispatch.JAXBDispatch.toReturnValue(JAXBDispatch.java:74)**
        at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:191)
        at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:195)
SANN3
  • 9,459
  • 6
  • 61
  • 97
Fakrudeen
  • 5,778
  • 7
  • 44
  • 70

5 Answers5

63

If you want to turn on logging on the server side, you need to register the LoggingFilter Jersey filter (on the container side).

This filter will log request/response headers and entities.

Here's what you need to add to your ResourceConfig class:

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Resources.
        packages(MyResource.class.getPackage().getName());

        register(LoggingFilter.class);    
    }
}

Note that the same filter also works on the client side.

Client client = Client.create();
client.addFilter(new LoggingFilter());
Nicola Isotta
  • 202
  • 3
  • 10
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
33

Jersey 2 has deprecated LoggingFilter and you now need to use LoggingFeature. In order to use it with a client you can use the following snipette:

this.client = ClientBuilder
            .newBuilder()
            .property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY)
            .property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_CLIENT, "WARNING")
            .build();

and on the server side:

ResourceConfig config = new ResourceConfig(HelloWorldResource.class);
config.register(LoggingFeature.class);
Art
  • 5,864
  • 3
  • 30
  • 32
10

Jersey 2.0 uses org.glassfish.jersey.filter.LoggingFilter
You can connect it with help of web.xml

<!-- Register my custom provider (not needed if it's in my.package) AND LoggingFilter. -->
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
        </init-param>

More explanations can be found here

upd:

After version 2.23 LoggingFilter is deprecated and LoggingFeature should be used. More info can be found in official documentation

bernard paulus
  • 1,644
  • 1
  • 21
  • 33
lanwen
  • 2,251
  • 1
  • 17
  • 30
6

For Jersey 1.2 add the following entry into web.xml inside the servlet tag:

    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
    </init-param>
eeezyy
  • 2,109
  • 1
  • 15
  • 18
  • 1
    would you please also mention where the log files are? – inor Jul 23 '17 at 04:48
  • 1
    it depends how and if you have configured your logging. you can look for a log4j.xml or log4j.properties, ... as default, the logging is just printed on System.out. – eeezyy Jul 23 '17 at 18:22
3

Could you show us your client code and tell us about the request as well?

This exception seems to point at the JAXB unmarshalling step. Apparently you received some XML from your REST API, but you don't get what you're waiting for.

Maybe the XSD you're using for marshalling/unmarshalling is outdated or just plain wrong.
Maybe you're trying to get the wrong entity from the response.

Try these steps and give us some more details about your problem:

Get the XML from the response

Using a REST client like Client REST simple (a chrome extension), or your code:

Builder builder = webResource.path("/yourapi/").accept("application/xml");

// get the client response
ClientResponse response = builder.get(ClientResponse.class);

// log the HTTP Status
logger.log("HTTP Status: " + response.getStatus());

// bypass the jaxb step and get the full response
// MyResource myResource = response.getEntity(MyResource.class);
String myResource = response.getEntity(String.class);
logger.log(myResource);

Validate this XML with the XSD you're using

This test should fail (if I'm right).

Jay
  • 476
  • 1
  • 5
  • 21
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • 1
    thanks Brian. This particular issue was due to invalid construction of URI. I made something like [look at mutation] http://localhost/blah/http://localhost/blah. It was hitting the server and server returned an error code and client failed due to library not handling null. com.sun.xml.internal.ws.message.AbstractMessageImpl:107 return (T) unmarshaller.unmarshal(readPayloadAsSource()); readPayloadAsSource() was returning null. So the exception message was more of red herring. I was mainly interested in knowing how to get jersey logs at server - so that I will know what server is returning. – Fakrudeen Feb 26 '10 at 16:17
  • Hey Fakrudeen. You really should edit your question to make that clear. I added another answer (I hope this one answsers your question). – Brian Clozel Mar 02 '10 at 09:35
  • Thanks! that was what I was looking for. BTW, I thought my very first line says ["How do I enable jersey logs at server side?"] what I want . – Fakrudeen Mar 02 '10 at 11:28
  • Agree. But then you elaborate about a JAXB problem on the client side - that's why I thought I was on the right path when I submitted my first answer. Don't get me wrong though, I think both questions and answers are relevant (and can be useful to jersey users) - rewriting part of it could help others. – Brian Clozel Mar 02 '10 at 12:28
  • @Brian i have added the init-param in the web.xml, i get the headers but not the actual request/response body. Is there something i am missing? – rajn Dec 05 '13 at 02:41