java - Springboot javax.xml.ws.service 实例化时出现 NullpointerException

标签 java spring-boot soap wsdl webservice-client

我正在使用 springboot 开发后端。我需要在我的 springboot 应用程序中实现一个 SOAP 客户端,但我面临一个问题,我无法理解它为何被引发以及如何引发。

@WebServiceClient(name = "WSCryptDecrypt", targetNamespace = "example", wsdlLocation = "Example")
public class WSCryptDecrypt extends Service {

    private final static QName WSCRYPTDECRYPT_QNAME = new QName("Example", "WSCryptDecrypt");

    public WSCryptDecrypt(URL wsdlLocation) {
        super(wsdlLocation, WSCRYPTDECRYPT_QNAME);
    }
}

我像这样实例化这个类:

WSCryptDecrypt wsCryptDecrypt = new WSCryptDecrypt(new URL("<WSDL-URL>"));

但我收到此错误:

Caused by: java.lang.NullPointerException: null
at javax.xml.ws.Service.<init>(Service.java:112) ~[jaxws-api-2.3.1.jar:na]
at com.mybackend.model.WSCryptDecrypt.<init>(WSCryptDecrypt.java:43) ~[classes/:na]

我不明白为什么以及如何引发此错误。我作为参数传递的 wsdl 的 url 肯定是正确的。我在springboot环境之外尝试了代码,效果很好。 Springboot 相反提示抛出此错误。

更新:

正如@Laurens建议的那样,我尝试了这种方法:

@Component注释WSCryptDecrypt类,然后添加到我喜欢的WsCryptDecryptService类中

@Autowired
WSCryptDecrypt wsCryptDecrypt;

此外,我用 @Service 注释了 WsCryptDecryptService 类

更新2:

javax.xml.ws.service 类在实例化时调用 this.getClass()。 也许这就是错误,Spring 还没有创建 Service 对象,所以 this 为 null。但我不知道如何解决这个问题。

更新3: 新的完全更新的代码:

@Component
@WebServiceClient(name = "WSCryptDecrypt", targetNamespace = "example", wsdlLocation = "Example")
public class WSCryptDecrypt extends Service {

    private final static QName WSCRYPTDECRYPT_QNAME = new QName("Example", "WSCryptDecrypt");

    public WSCryptDecrypt(URL wsdlLocation) {
        // Here it throws the error
        super(wsdlLocation, WSCRYPTDECRYPT_QNAME);
    }
}

服务等级

@Service
public class WsCryptDecryptService {

  //this is the soap service
  private WSCryptDecryptSoap wsCryptDecryptSoap;

  @Autowired
  WSCryptDecrypt wsCryptDecrypt;

   /**
   * Creates the service pointing to TEST Environment.
   * @throws MalformedURLException
   */
  public WsCryptDecryptService() {
    wsCryptDecryptSoap = wsCryptDecrypt.getWSCryptDecryptSoap();
  }
}

更新4

我想这可能是依赖性的问题。这些是我在 javax.xml.ws 的 pom 中放入的依赖项(它们超出了需要,只是因为我想检查是否加载了所有可能的库)

    <dependency>
        <groupId>javax.jws</groupId>
        <artifactId>javax.jws-api</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.ws</groupId>
        <artifactId>jaxws-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.jws</groupId>
        <artifactId>jsr181</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.jws</groupId>
        <artifactId>com.springsource.javax.jws</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.jws.jsr181-api</groupId>
        <artifactId>jsr181-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.servicemix.bundles</groupId>
        <artifactId>org.apache.servicemix.bundles.jaxws-api-2.0</artifactId>
        <version>4.0-m1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>rt</artifactId>
        <version>2.3.1</version>
    </dependency>

最佳答案

不要实例化您的服务,让 Spring 使用 @Autowired 来处理这个问题。我不建议使用构造函数,因为这是不好的做法,只需在需要请求 WSCryptDecryptSoap 时调用该方法

@Service
public class WsCryptDecryptService {

    @Autowired
    private WSCryptDecrypt wsCryptDecrypt;

    public WSCryptDecryptSoap getWSCryptDecryptSoap() {
        return wsCryptDecrypt.getWSCryptDecryptSoap();
    }
}

关于java - Springboot javax.xml.ws.service 实例化时出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54397280/

相关文章:

java - Kotlin:在内部使用可变时从 getter 返回不可变集合

java - 多模块项目中测试类的正确位置是什么?

java - 使用 SOAPHandler 获取 SOAP 响应的请求

web-services - 使用 fiddler 捕获 Web 服务 xml 请求

c# - 使用 c# 和 mono 关闭 linux

java - 结合 CompletableFuture 和 JdbcTamplate 的正确方法是什么?

java - Glassfish Netbeans 附加到本地主机 "Connection refused"

java - EodData wsdl java连接

spring-boot - 我无法在 spring boot 中从 yaml 注入(inject) Map<String, String>

java - Jersey 、Spring Boot、上传图像不支持的媒体类型