如何修复javax.xml.ws.WebServiceException:java.lang.NullPointerException?

时间:2016-02-10 07:16:58

标签: java web-services spring-boot cxf

我正在运行Spring Boot应用程序(mvn:spring-boot:run)并获得下一个堆栈跟踪:

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.2.RELEASE)

2016-02-10 09:57:31.350  INFO 8868 --- [           main] com.comp.config.Application         : Starting Application on SOFT12 with PID 8868 (C:\Users\Maya\git\app-services\target\classes started by Maya in C:\Users\Maya\git\app-services)
2016-02-10 09:57:31.356  INFO 8868 --- [           main] com.comp.config.Application         : No active profile set, falling back to default profiles: default
2016-02-10 09:57:31.429  INFO 8868 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7dd7f653: startup date [Wed Feb 10 09:57:31 MSK 2016]; root of context hierarchy
2016-02-10 09:57:33.174  INFO 8868 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-02-10 09:57:35.126  INFO 8868 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-02-10 09:57:35.151  INFO 8868 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-02-10 09:57:35.153  INFO 8868 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-02-10 09:57:35.296  INFO 8868 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-02-10 09:57:35.296  INFO 8868 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3871 ms
2016-02-10 09:57:36.401  INFO 8868 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'CXFServlet' to [/APPservice/*]
2016-02-10 09:57:36.408  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'metricFilter' to: [/*]
2016-02-10 09:57:36.409  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-02-10 09:57:36.409  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-02-10 09:57:36.409  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-02-10 09:57:36.410  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-02-10 09:57:36.410  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2016-02-10 09:57:36.410  INFO 8868 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'applicationContextIdFilter' to: [/*]
2016-02-10 09:57:37.028  INFO 8868 --- [           main] o.a.c.w.s.f.ReflectionServiceFactoryBean : Creating Service {http://new.webservice.namespace}CompServiceForPCO from WSDL: classpath:CompService.wsdl
2016-02-10 09:57:39.665  WARN 8868 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ws' defined in com.comp.config.Application: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.xml.ws.Endpoint]: Factory method 'ws' threw exception; nested exception is javax.xml.ws.WebServiceException: java.lang.NullPointerException
2016-02-10 09:57:39.676  INFO 8868 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2016-02-10 09:57:39.891 ERROR 8868 --- [           main] o.s.boot.SpringApplication               : Application startup failed

ws方法在Application.class中,如下所示:

package com.comp.config;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import com.comp.pcoserv.CompServiceEndPoindImpl;

import javax.xml.ws.Endpoint;


@Configuration
@EnableAutoConfiguration
public class Application  {

    public static final String SERVLET_MAPPING_URL_PATH = "/APPservice";
    public static final String SERVICE_NAME_URL_PATH = "/ws";

    @Autowired
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), SERVLET_MAPPING_URL_PATH + "/*");
    }


    @Bean(name = Bus.DEFAULT_BUS_ID)
    /* <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus">*/
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    /* <jaxws:endpoint id="app" implementor="com.dlizarra.app.ws.AppImpl" address="/app">*/
    public Endpoint ws() {
        //Bus bus = (Bus) applicationContext.getBean(Bus.DEFAULT_BUS_ID);


        Object implementor = new CompServiceEndPoindImpl();
        EndpointImpl endpoint = new EndpointImpl(springBus(), implementor);
        endpoint.publish(SERVICE_NAME_URL_PATH);
        endpoint.setWsdlLocation("CompService.wsdl");
        /*endpoint.getServer().getEndpoint().getInInterceptors().add(new LoggingInInterceptor());
        endpoint.getServer().getEndpoint().getOutInterceptors().add(new LoggingOutInterceptor());*/
        return endpoint;
    }

}

我搜索了整个互联网以找到解决方案但没有任何成功。你能帮我解决这个例外吗?

完整堆栈跟踪在此处:full stack trace

3 个答案:

答案 0 :(得分:4)

当您的服务返回的对象没有空构造函数时,可能会发生此错误。

确保你所有的dto都是空构造函数。

答案 1 :(得分:2)

问题在于方法ws()中的ENDPOINT的Instatiation。所以在这一行之后

EndpointImpl endpoint = new EndpointImpl(springBus(), implementor);

在其上调用publish会导致NullPointerException

错误追踪: -

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.xml.ws.Endpoint]: Factory method 'ws' threw exception; nested exception is javax.xml.ws.WebServiceException: java.lang.NullPointerException

Caused by: javax.xml.ws.WebServiceException: java.lang.NullPointerException
    at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:375) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.EndpointImpl.publish(EndpointImpl.java:255) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]

扩展错误追踪: -

Caused by: java.lang.NullPointerException: null
    at org.apache.cxf.common.util.ASMHelper.getClassCode(ASMHelper.java:212) ~[cxf-core-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.WrapperClassGenerator.generateMessagePart(WrapperClassGenerator.java:310) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.WrapperClassGenerator.createWrapperClass(WrapperClassGenerator.java:224) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.WrapperClassGenerator.generate(WrapperClassGenerator.java:132) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.generatedWrapperBeanClass(JaxWsServiceFactoryBean.java:675) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.getExtraClass(JaxWsServiceFactoryBean.java:645) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean.buildServiceFromWSDL(ReflectionServiceFactoryBean.java:417) ~[cxf-rt-wsdl-3.1.5.jar:3.1.5]
    at org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:525) ~[cxf-rt-wsdl-3.1.5.jar:3.1.5]
    at org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:261) ~[cxf-rt-wsdl-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:199) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:102) ~[cxf-rt-frontend-simple-3.1.5.jar:3.1.5]
    at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:168) ~[cxf-rt-frontend-simple-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:211) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.EndpointImpl.getServer(EndpointImpl.java:460) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    at org.apache.cxf.jaxws.EndpointImpl.doPublish(EndpointImpl.java:338) ~[cxf-rt-frontend-jaxws-3.1.5.jar:3.1.5]
    ... 37 common frames omitted

org.apache.cxf.common.util.ASMHelper: -

public static String getClassCode(Class<?> cl) {
        if (cl == Void.TYPE) {
            return "V";
        }
        if (cl.isPrimitive()) {

答案 2 :(得分:2)

就我而言,问题出在ServiceInterface类的注释中:需要

@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) 

而不是

@SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)

用于某些方法。