简体   繁体   中英

Springboot javax.xml.ws.service Nullpointerexception while instantiating it

I am developing a backend using springboot. I need to implement a soap client within my springboot application but I am facing an issue that I cannot understand why it is raised and how.

@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);
    }
}

I instantiate this class like this:

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

But I get this error:

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]

I don't get why and how this error is thrown. The url of the wsdl, that I pass as parameter, is correct for sure. I tried the code outside the springboot environmente and it works well. Springboot instead is complaining throwing this error.

UPDATE:

As @Laurens suggested I tried this approach:

Annotate WSCryptDecrypt class with @Component and then into WsCryptDecryptService class I do like that

@Autowired
WSCryptDecrypt wsCryptDecrypt;

In addition I annotated WsCryptDecryptService class with @Service

UPDATE 2:

javax.xml.ws.service class when instantiated it calls this.getClass(). Maybe this is the error, Spring did not create the Service object yet, so this is null. But I don't know how may I fix that.

UPDATE 3: New fully updated code:

@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 class

@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();
  }
}

UPDATE 4

I thought that maybe it is a problem of dependency. Those are the dependency that I put in my pom for javax.xml.ws (They are more than needed just because I wanted to check that I load all the possible libraries)

    <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>

Don't instantiate your services, let Spring take care of this using @Autowired. I wouldn't recommend using a constructor as this is bad practise, just call the method when you need to request your WSCryptDecryptSoap

@Service
public class WsCryptDecryptService {

    @Autowired
    private WSCryptDecrypt wsCryptDecrypt;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM