0
votes

I'm using java 8. I'm using jersey 2.26 as jax-rs implementation. Everything works fine, but I'm not being able to log.

I have generated WS stub with eclipse:

File -> new project -> maven prokect -> filtered jersey 2.26 -> inserted my groupid and artifactid -> finish.

The following is my resource class:

package mypackage;

import ...

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

    @EJB
    private Service service;

    private Logger logger = Logger.getLogger(getClass().toString());

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "application/json" media type.
     *
     * @return MyObject that will be returned as a application/json response.
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Logged
    public MyObject getIt() {
        logger.info("LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG");
        //some computation
        return result;
    }

}

The following is my LoggingFilter:

package log;

import ...


@Logged
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {

    private Logger logger = Logger.getLogger(getClass().toString());

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
            throws IOException {
        logger.info("RESPOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONSE");
        logger.info(requestContext.toString());
        logger.info(responseContext.toString());

    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        logger.info("REQUEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEST");
        logger.info(requestContext.toString());
    }

}

The following is my interface:

package log;

import ...

import javax.ws.rs.NameBinding;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@NameBinding
public @interface Logged { }

The following is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>mypackage</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

filter methods in LogginFilter are never called. "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG" is printed, however.

2

2 Answers

0
votes

This site is the Bible: https://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/

You have to choose between:

  1. register your jersey providers in web.xml
  2. create application extending resourceConfig.

I chose the first possibility.

0
votes

Two things you need. First, with the following

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>mypackage</param-value>
</init-param>

You only list mypackage as the package to scan but the filter is in the log package. You could add the log package delimited with a comma.

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>mypackage,log</param-value>
</init-param>

But generally the way you should package your application is to have at least a root package and all other packages are subpackages. For example

com.foo.resources
com.foo.filters

Here com.foo is the root package. What you can do here is just list com.foo as the package to scan and Jersey will recursively scan it, including com.foo.resources and com.foo.filters

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.foo</param-value>
</init-param>

Second thing you need is @Provider annotation on the filter. The package scanning looks for @Path and @Provider annotated classes to register.