我正在尝试激活Web服务的监控统计信息。但我得到空指针异常。如何正确激活?我不明白球衣的文件。这是我的代码:
package com.app.rest;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.monitoring.MonitoringStatistics;
import org.glassfish.jersey.server.monitoring.TimeWindowStatistics;
@Path("/")
public class TestingEvents extends ResourceConfig {
public TestingEvents()
{
ResourceConfig cnf = new ResourceConfig(TestingEvents.class);
cnf.property(ServerProperties.MONITORING_ENABLED, true);
}
@Inject
Provider<MonitoringStatistics> monitoringStatisticsProvider;
@Path("/test")
@GET
public void test()
{
System.out.println("Invoked.........");
final MonitoringStatistics snapshot = monitoringStatisticsProvider.get();
System.out.println(snapshot.getRequestStatistics().toString());
// final TimeWindowStatistics timeWindowStatistics = snapshot.getRequestStatistics().getTimeWindowStatistics().get(0);
//System.out.println("request count: " + timeWindowStatistics.getRequestCount()+", average request processing [ms]: " + timeWindowStatistics.getAverageDuration());
}}
和我的网页xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>RestCounter</display-name>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.app.rest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/myApp/*</url-pattern>
</servlet-mapping>
例外:
SEVERE: Servlet.service() for servlet [MyApplication] in context with path [/RestCounter] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.app.rest.TestingEvents.test(TestingEvents.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:143)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:160)
at org.glassfish.jersey.server.model.internal.VoidVoidDispatcherProvider$VoidToVoidDispatcher.doDispatch(VoidVoidDispatcherProvider.java:78)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
调用显示在控制台上,然后程序崩溃。求助。
答案 0 :(得分:2)
首先得到一些配置方面......
ResourceConfig
是web.xml的替代品。它们可以一起使用,因为您可以配置web.xml以使用您的ResourceConfig
子类,但我很少看到需要。如果您不想使用web.xml,可以执行类似
@ApplicationPath("/myApp")
public class AppConfig extends ResourceConfig {
public AppConfig() {
packages("com.app.rest");
}
}
这相当于您声明ServletContainer
,设置要扫描的包,并将网址映射设置为/myApp/*
。使用上面的配置,您可以完全摆脱web.xml(假设您具有jersey-container-servlet
依赖关系,并且您处于Servlet 3.x环境中;在Servlet 2.x环境中,您需要使用web.xml)。
您不做的一件事就是让您的资源类扩展ResourceConfig
。所以摆脱子类(在你的TestingEvents
类中),绝对摆脱你在构造函数中的内容。
如果您决定使用ResourceConfig
子类......
你可以简单地做到
@ApplicationPath("/myApp")
public class AppConfig extends ResourceConfig {
public AppConfig() {
packages("com.app.rest");
property(ServerProperties.MONITORING_STATISTICS_ENABLED, true);
}
}
如果您决定使用web.xml
...
你应该保持javadoc of the ServerProperties
方便。每个静态字段都有String值,这些值大部分可以在web.xml中配置为Jersey Servlet的<init-param>
。如果查看ServerProperties.MONITORING_STATISTICS_ENABLED
,您会看到String值为
jersey.config.server.monitoring.statistics.enabled
因此要将其设置为init-param,请执行此操作
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.app.rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.monitoring.statistics.enabled</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>