constructor Service in class javax.xml.ws.Service

2019-04-30 03:01发布

问题:

I have created a web service with apache-cxf-2.7.4. I entered the classes produced in my project. the libraries I have in my project are:

  • math3-commons-3.2.jar
  • XStream-1.4.4.jar
  • jaxws-api-2.2.5.jar

I have the following error:

  constructor Service in class javax.xml.ws.Service cannot be applied to given types;
  required: java.net.URL,javax.xml.namespace.QName
  found: java.net.URL,javax.xml.namespace.QName,javax.xml.ws.WebServiceFeature[]
  reason: actual and formal argument lists differ in length

回答1:

The problem is the version of JAX-WS API. The classloader for your application first loaded the version included in Java SE or Java EE.

For Java SE 6 or Java EE 5, JAX-WS API 2.1. The constructors in javax.xml.ws.Service:

javax.xml.ws.Service.Service(URL, QName)

For Java SE 7 or Java EE 6, JAX-WS API 2.2. The constructors in javax.xml.ws.Service:

javax.xml.ws.Service.Service(URL, QName)
javax.xml.ws.Service.Service(URL, QName, WebServiceFeature...)  // You need this!

There are three possible solutions (depends on whether it is a web application or standalone application):

  1. Use Java SE 7 or Java EE 6.

  2. Re-run wsdl2java with argument -frontend jaxws21 to generate JAX-WS 2.1 compliant code instead.

  3. Change the classloader for load first the classes included in the application.



回答2:

If using Maven to build you should add this to the execution configuration

<defaultOptions>
    <extraargs>
        <extraarg>-frontend</extraarg>
        <extraarg>jaxws21</extraarg>
    </extraargs>
</defaultOptions>

(thanks to Paul Vargas for pointing me in the right direction).



回答3:

wsimport -help tells us about the -target option. It says: Generate code as per the given JAXWS spec version. Defaults to 2.2, Accepted values are 2.0, 2.1 and 2.2

If you are using jdk wsimport tool, then just add the -target argument like below.

wsimport -keep -d \myDirToStoreExtractedClientCode -target 2.1 \myWSDLlocation\mineNotYours.wsdl

(Thanks Paul Vargas for helping out, old post but still helpful.)