为什么没有执行LoggingFilter?

时间:2018-04-09 11:18:00

标签: java logging interface jersey jax-rs

我正在使用java 8.我使用jersey 2.26作为jax-rs实现。一切正常,但我无法登录。

我用eclipse生成了WS stub:

档案 - >新项目 - > maven prokect - >过滤后的运动衫2.26 - >插入我的groupid和artifactid - >光洁度。

以下是我的资源类:

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;
    }

}

以下是我的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());
    }

}

以下是我的界面:

package log;

import ...

import javax.ws.rs.NameBinding;

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

以下是我的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>
永远不会调用LogginFilter中的

过滤器方法。然而,打印“LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO”。

2 个答案:

答案 0 :(得分:0)

这个网站是圣经:https://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/

你必须在:

之间做出选择
  1. 在web.xml中注册您的球衣提供者
  2. 创建应用程序扩展resourceConfig。
  3. 我选择了第一种可能性。

答案 1 :(得分:0)

你需要的两件事。首先,使用以下内容

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

您只列出mypackage作为要扫描的包,但过滤器位于log包中。您可以添加用逗号分隔的log包。

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

但一般来说,应该打包应用程序的方式是至少拥有一个root包,所有其他包都是子包。例如

com.foo.resources
com.foo.filters

这里com.foo是根包。你可以在这里做的只是列表com.foo作为要扫描的包,Jersey将递归扫描它,包括com.foo.resourcescom.foo.filters

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

您需要的第二件事是过滤器上的@Provider注释。包扫描会查找要注册的@Path@Provider注释类。