我的代码看起来像
public class IpIdActivityMockResponse {
final String response;
public String getResponse() {
return response;
}
public IpIdActivityMockResponse() {
final InputStream stream = ClassLoader.getSystemResourceAsStream("/response_little.xml");
response = convertStreamToString(stream);
}
private static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
和REST
端点为
@Path("/activities")
public class StealthWatch {
@GET
@Produces("application/xml")
public Response getActivities() {
return Response
.ok(new IpIdActivityMockResponse().getResponse())
.build();
}
}
我使用maven
将war
捆绑为
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>stealthwatch</warName>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/webapp</directory>
</resource>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/resources</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
当我在wildfly 9
容器上部署它时,点击API
curl -v http://127.0.0.1:8080/stealthwatch/rest/activities
我将错误视为
11:52:44,611 ERROR [io.undertow.request] (default task-18) UT005023: Exception handling request to /stealthwatch/rest/activities: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:168)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:411)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:202)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at java.util.Scanner.<init>(Scanner.java:563)
at IpIdActivityMockResponse.convertStreamToString(IpIdActivityMockResponse.java:33)
at IpIdActivityMockResponse.<init>(IpIdActivityMockResponse.java:29)
at StealthWatch.getActivities(StealthWatch.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:395)
... 32 more
我也看到我的xml
在战争中
$ jar -tvf target/stealthwatch.war
0 Fri Jan 08 11:52:36 PST 2016 META-INF/
130 Fri Jan 08 11:52:36 PST 2016 META-INF/MANIFEST.MF
0 Fri Jan 08 11:52:36 PST 2016 WEB-INF/
0 Fri Jan 08 11:52:36 PST 2016 WEB-INF/classes/
0 Fri Jan 08 11:52:36 PST 2016 WEB-INF/classes/dto/
0 Fri Jan 08 11:52:36 PST 2016 WEB-INF/lib/
896 Fri Jan 08 09:40:14 PST 2016 response.xml
162 Fri Jan 08 09:47:40 PST 2016 response_little.xml
984 Fri Jan 08 11:52:34 PST 2016 WEB-INF/classes/dto/StealthWatchResponse.class
1811 Fri Jan 08 11:52:34 PST 2016 WEB-INF/classes/IpIdActivityMockResponse.class
896 Fri Jan 08 11:52:34 PST 2016 WEB-INF/classes/response.xml
162 Fri Jan 08 11:52:34 PST 2016 WEB-INF/classes/response_little.xml
873 Fri Jan 08 11:52:34 PST 2016 WEB-INF/classes/StealthWatch.class
46968 Fri Jan 08 09:39:16 PST 2016 WEB-INF/lib/jackson-annotations-2.6.0.jar
258875 Fri Jan 08 09:39:16 PST 2016 WEB-INF/lib/jackson-core-2.6.3.jar
1170801 Fri Jan 08 09:39:16 PST 2016 WEB-INF/lib/jackson-databind-2.6.3.jar
93734 Fri Jan 08 09:39:16 PST 2016 WEB-INF/lib/jackson-dataformat-xml-2.6.3.jar
32618 Fri Jan 08 09:39:16 PST 2016 WEB-INF/lib/jackson-module-jaxb-annotations-2.6.3.jar
57193 Thu Jan 07 15:46:24 PST 2016 WEB-INF/lib/jboss-logging-3.1.4.GA.jar
152036 Thu Jan 07 15:46:26 PST 2016 WEB-INF/lib/resteasy-client-3.0.14.Final.jar
161867 Fri Jan 08 09:39:16 PST 2016 WEB-INF/lib/stax2-api-3.1.4.jar
1114 Fri Jan 08 10:06:44 PST 2016 WEB-INF/web.xml
2285 Fri Jan 08 11:50:22 PST 2016 META-INF/maven/com.lancope/stealthwatch/pom.xml
122 Fri Jan 08 11:52:36 PST 2016 META-INF/maven/com.lancope/stealthwatch/pom.properties
我做错了什么?
答案 0 :(得分:2)
尝试使用
final InputStream stream = getClass().getResourceAsStream("/response_little.xml")
答案 1 :(得分:0)
试试这个:
final InputStream stream = IpIdActivityMockResponse.class.getClassLoader().getResourceAsStream("/response_little.xml");