Java调用Web服务问题 - javax.servlet.ServletException:java.lang.NullPointerException

时间:2018-03-28 19:36:52

标签: java servlets web service jersey

我在拨打该服务时收到错误消息。我尝试了所有可以找到的解决方案

错误全文:

  

SEVERE:servlet [Jersey RESTful Application]的Servlet.service()在路径[/ sove]的上下文中引发了带有根本原因的异常[java.lang.NullPointerException]   显示java.lang.NullPointerException       at com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl.useNamespace(ListTransducedAccessorImpl.java:63)       at com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty.hasSerializeURIAction(AttributeProperty.java:96)       at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.link(ClassBeanInfoImpl.java:232)       在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。(JAXBContextImpl.java:348)       在com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl。(JAXBContextImpl.java:124)       at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl $ JAXBContextBuilder.build(JAXBContextImpl.java:1123)       at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)       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:498)       在javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)       在javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)       在javax.xml.bind.ContextFinder.find(ContextFinder.java:462)       在javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)       在javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)       at org.glassfish.jersey.jaxb.in​​ternal.AbstractJaxbProvider.getStoredJaxbContext(AbstractJaxbProvider.java:259)       在org.glassfish.jersey.jaxb.in​​ternal.AbstractJaxbProvider.getJAXBContext(AbstractJaxbProvider.java:251)       在org.glassfish.jersey.jaxb.in​​ternal.AbstractJaxbProvider.getMarshaller(AbstractJaxbProvider.java:218)       在org.glassfish.jersey.jaxb.in​​ternal.AbstractJaxbProvider.getMarshaller(AbstractJaxbProvider.java:185)       at org.glassfish.jersey.jaxb.in​​ternal.AbstractRootElementJaxbProvider.writeTo(AbstractRootElementJaxbProvider.java:163)       at org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)       at org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)       在org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)       在org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106)       在org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)       at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86)       在org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)       在org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1128)       at org.glassfish.jersey.server.ServerRuntime $ Responder.writeResponse(ServerRuntime.java:664)       at org.glassfish.jersey.server.ServerRuntime $ Responder.processResponse(ServerRuntime.java:421)       在org.glassfish.jersey.server.ServerRuntime $ Responder.process(ServerRuntime.java:411)       在org.glassfish.jersey.server.ServerRuntime $ 2.run(ServerRuntime.java:308)       在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)       在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)       在org.glassfish.jersey.internal.Errors.process(Errors.java:315)       在org.glassfish.jersey.internal.Errors.process(Errors.java:297)       在org.glassfish.jersey.internal.Errors.process(Errors.java:267)       在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)       在org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)       在org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)       在org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)       在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)       在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)       在org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:861)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1455)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:748)

我有一个jax-rs网络服务,如:

@GET
@Path("/getAnalyzedMatches")
@Produces(MediaType.APPLICATION_XML)
public static AnalyzeResponseModel getAnalyzedMatches() throws ClassNotFoundException, SQLException {
    AnalyzeResponseModel response = new AnalyzeResponseModel();
    List<AnalyseResultModel> analyzedMatches = SoveMatchesUtil.getAnalyzedMatches();
    /* try to set object */
    List<Object> objectList = new ArrayList<>();
    for (AnalyseResultModel item : analyzedMatches) {
        objectList.add(item);
    }
    response.setList(objectList);
    return response;
}

这是我的响应模型:

package com.sove.model;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "list")
public class AnalyzeResponseModel {
    List<Object> list;

    public AnalyzeResponseModel() {
    }

    public List<Object> getList() {
        return list;
    }

    @XmlAttribute
    public void setList(List<Object> list) {
        this.list = list;
    }

}

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sove</groupId>
<artifactId>sove</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>3.0.12.Final</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.bundles</groupId>
        <artifactId>jaxrs-ri</artifactId>
        <version>2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.lucee/postgresql -->
    <dependency>
        <groupId>org.lucee</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.3-606.jdbc4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

</dependencies>

这是我的web.xml

<?xml version = "1.0" encoding = "UTF-8"?> 
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
   xmlns = "http://java.sun.com/xml/ns/javaee"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
   id = "WebApp_ID" version = "3.0"> 
   <display-name>sove</display-name> 
   <servlet> 
      <servlet-name>Jersey RESTful 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>com.sove.service</param-value> 
      </init-param> 
   </servlet> 
   <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/*</url-pattern> 
   </servlet-mapping>   
</web-app>

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

通过在响应模型(AnalyseResultModel)中将公共属性设置为private来解决问题。