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.