gpt4 book ai didi

Java Jersey 如何阻止loggingFilter打印?

转载 作者:行者123 更新时间:2023-11-30 06:16:53 24 4
gpt4 key购买 nike

我正在使用 jersey loggingFilter 类来打印所有请求/响应内容。这是过滤器连接代码:

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

import com.ladpc.mobile.util.CORSResponseFilter;
import com.ladpc.mobile.util.LoggingResponseFilter;

/**
* Registers the components to be used by the JAX-RS application
*
* @author ama
*
*/
public class MobileApplication extends ResourceConfig {

/**
* Register JAX-RS application components.
*/
public MobileApplication() {
try {
register(RequestContextFilter.class);
register(TaskService.class);
register(LookupService.class);
register(UsersService.class);
register(PropertyService.class);
register(DocumentsPathService.class);
register(DocumentsService.class);
register(WorkPlanService.class);
register(IssueService.class);
register(ActionTypesService.class);
register(JacksonFeature.class);
register(LoggingResponseFilter.class);
register(MyApplicationEventListener.class);
registerInstances(new LoggingFilter(Logger.getLogger(MobileApplication.class.getName()), true));
register(CORSResponseFilter.class);
register(MultiPartFeature.class);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

问题:某些响应内容太长,因为它包含图像内容或 pdf 文件内容的字节数组,因此当客户端多次调用此服务时,日志文件会被填满并导致许多运行问​​题。有没有一种方法可以阻止某些服务,这些服务没有由loggingFilter类自动打印响应?

谢谢。

最佳答案

我发现了...在这一行中:

registerInstances(new LoggingFilter(Logger.getLogger(MobileApplication.class.getName()), true));

有 c-tor 调用,第二个参数是 'boolean printEntity' - 如果要打印响应内容则为 true,否则为 false...

因此将此行更改为:

 registerInstances(new LoggingFilter(Logger.getLogger(MobileApplication.class.getName()), false));

可以解决这个问题。

关于Java Jersey 如何阻止loggingFilter打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48989368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com