Troubleshooting a JAX-RS application

When developing a JAX-RS application, it's often helpful to see what the runtime is doing if you run in to problems with your client or service. Here are a few tips for how to go about troubleshooting your application.

Enabling commons-logging

For this example, we'll work with a set of JAXB bean tha Include instructions for turning on logging in certain environments.

Parameter values are null or not available

If you find that your method is being properly targeted, but that your parameter values are null, it could be that you have the wrong type of parameter specified. Let's take the example of a method defined as it is below.

@GET
@Path(value="/people/{id}")
public Person getPerson(@QueryParam(value="id") String id) {
	    
    // look-up person info based on "id" param
	    
}

If this operation is invoked with the URL: http://myhost/people/123 the parameter that is passed in for the String value will be null. This is because the parameter is defined in the method signature using the QueryParam annotation instead of as a PathParam. For that URL to work, the method should be defined as:

@GET
@Path(value="/people/{id}")
public Person getPerson(@PathParam(value="id") String id) {

    // look-up person info based on "id" param
    
}

Or, another option is to change the invocation URL to use the HTTP query string parameter like: http://myhost/people?id=123

Another pattern that could be causing your parameters to be null is how they are defined in the parameter annotation. You might have accidentally included "{" and "}" around your parameter id. This would result in the runtime not being able to match the parameter value with the id you have specified.

Bad:

@PathParam(value="{id}")

Good:

@PathParam(value="id")

Implemented Provider interface but Provider does not work

Sometimes you've implemented a provider interface but the provider does not seem to work. Be sure to add the javax.ws.rs.ext.Provider annotation to the provider class and add the provider class to your javax.ws.rs.core.Application class methods.