kevinarpe
kevinarpe

Reputation: 21319

What is the purpose of javax.ws.rs.container.ResourceInfo.getResourceClass()?

I am writing a custom Jersey 2.0 server logging filter.

Sample: https://github.com/jersey/jersey/blob/master/core-common/src/main/java/org/glassfish/jersey/logging/ServerLoggingFilter.java

A reference to ResourceInfo is injected as:

@Context
private ResourceInfo resourceInfo;

Interface ResourceInfo has two methods:

Is there ever a case where ResourceInfo.getResourceClass() != ResourceInfo.getResourceMethod().getDeclaringClass()?

Bonus question: Both methods may return null. Is it possible that only one would be null?

Upvotes: 2

Views: 2819

Answers (1)

varren
varren

Reputation: 14731

Can't say much about null values, but there is a case when getResourceClass() != getResourceMethod().getDeclaringClass() if you have some class hierarchy like superclass resource. It is easier to show in code, so consider this:

public class SuperResource {
    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    public Response getInfo() {
        return Response.ok("{\"info\":1}").build(); 
    }
}

@Path("test")
public class MyResource  extends SuperResource{
    // ... other endpoints
}

Now lets call

GET http://localhost:9998/test/

getResourceClass() : class ru.varren.MyResource
getResourceMethod(): javax.ws.rs.core.Response ru.varren.SuperResource.getInfo()

Upvotes: 4

Related Questions