无法封装类型“java.lang.Integer”作为元素,因为它缺少@XmlRootElement注释

时间:2015-07-05 01:11:15

标签: java maven jaxb spring-boot spring-ws

我的设置......

Apache Maven 3.0.4 (r1232337; 2012-01-17 08:44:56+0000)
Maven home: D:\duper\apache-maven-3.0.4
Java version: 1.8.0_20, vendor: Oracle Corporation
Java home: D:\duper\jdk1.8.0_20\jre
...

我们的第三方合作伙伴发布了我们需要调用的SOAP Web服务。

在spring.io的“ Consuming a SOAP web service ”指导下,我使用spring-ws构建了一个Web服务客户端。 Maven成功构建了客户端。意思是,如果我只运行clean install,maven就会成功完成构建......

mvn clean install..
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:31.938s
...

然而,当我尝试通过spring-boot运行成功编译的WS客户端时,它会发生轰炸......

mvn -e -X spring-boot:run
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.026s
...
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:run (default-cli) on project recruiterTest: An exception occured while running. null: InvocationTargetException: JAXB marshalling exception
[ERROR] - with linked exception:
[ERROR] [com.sun.istack.SAXException2: unable to marshal type "java.lang.Integer" as an element because it is missing an @XmlRootElement annotation]
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:run (default-cli) on project recruiterTest: An exception occured while running. null
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        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:483)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while running. null
        at org.springframework.boot.maven.RunMojo$IsolatedThreadGroup.rethrowUncaughtException(RunMojo.java:387)
        at org.springframework.boot.maven.RunMojo.runWithMavenJvm(RunMojo.java:214)
        at org.springframework.boot.maven.RunMojo.run(RunMojo.java:187)
        at org.springframework.boot.maven.RunMojo.execute(RunMojo.java:144)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 more
Caused by: java.lang.reflect.InvocationTargetException
        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:483)
        at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: unable to marshal type "java.lang.Integer" as an element because it is missing an @XmlRootElement annotation]
        at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:913)
        at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:689)
        at org.springframework.ws.support.MarshallingUtils.marshal(MarshallingUtils.java:81)
        at org.springframework.ws.client.core.WebServiceTemplate$2.doWithMessage(WebServiceTemplate.java:399)
        at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:590)
        at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
        at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)
        at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:383)
        at deduper.RecruiterWSClient.getEmployee(RecruiterWSClient.java:19)
        at deduper.App.main(App.java:19)
        ... 6 more
Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: unable to marshal type "java.lang.Integer" as an element because it is missing an @XmlRootElement annotation]
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
        at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:685)
        ... 14 more
Caused by: com.sun.istack.SAXException2: unable to marshal type "java.lang.Integer" as an element because it is missing an @XmlRootElement annotation
        at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:252)
        at com.sun.xml.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:141)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:498)
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
        ... 16 more
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
...

......这是主要应用程序的来源......

package deduper;

import recruiter.svc.hr.*;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;


public class App { 

   public static void main( String [] args ) { 

      ApplicationContext ctx = SpringApplication.run(RecruiterWSConfiguration.class, args);

      RecruiterWSClient recrWSClient = ctx.getBean(RecruiterWSClient.class);

/* NOTE TO SO — we don't have access to the original source code for recruiter.svc.hr.Employee */
/* WS consumers would only see the JAXB-marshalled implementation – in their own jvm  */
      Employee emp = recrWSClient.getEmployee( 6345789 );

      System.out.println( "[emp id = " + emp.getId( ) + " : job title = " + emp.getJobTitle( ) + " : emp name = " + emp.getEmployeeName( ) + "]" );

   }
}

......以及一些弹簧配置的来源......

package deduper;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class RecruiterWSConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("recruiter.svc.hr");
        return marshaller;
    }

    @Bean
    public RecruiterWSClient recrWSClient (Jaxb2Marshaller marshaller) {
        RecruiterWSClient client = new RecruiterWSClient();
        client.setDefaultUri("http://recruitment.biz:7001/technology/EmployeeSvc?WSDL");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

......等等......

package deduper;

import recruiter.svc.hr.Employee;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;

public class RecruiterWSClient extends WebServiceGatewaySupport{ 

public RecruiterWSClient( ) { }


  public Employee getEmployee( int id ) { 

/* NOTE TO SO — we don't have access to the original source code for recruiter.svc.hr.Employee */
/* WS consumers would only see the JAXB-marshalled implementation – in their own jvm  */
       return (Employee)getWebServiceTemplate().marshalSendAndReceive( id, new SoapActionCallback( "getEmployee()") );

    }

}

......这是pom ......

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>deduper</groupId>
    <artifactId>RecruiterWSTest</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>RecruiterWSTest Spring-WS Application</name>
    <url>http://www.springframework.org/spring-ws</url>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>RecruiterWSTest</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- tag::wsdl[] -->
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>recruiter.svc.hr</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://recruitment.biz:7001/technology/EmployeeSvc?WSDL</url>
                        </schema>
                    </schemas>                  
            <debug>true</debug>
            <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

...哦,是的。架构......

<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. -->
<xs:schema xmlns:tns="http://recruiter/svc/hr/api/ws" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://recruiter/svc/hr/api/ws">
    <xs:element name="employee" type="tns:employee"/>
    <xs:element name="getEmployee" type="tns:getEmployee"/>
    <xs:element name="getEmployeeResponse" type="tns:getEmployeeResponse"/>
    <xs:complexType name="getEmployee">
        <xs:sequence>
            <xs:element name="empId" type="xs:int" form="qualified"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="getEmployeeResponse">
        <xs:sequence>
            <xs:element name="Employee" type="tns:employee" form="qualified" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="employee">
        <xs:sequence>
            <xs:element name="employeeName" type="xs:string" minOccurs="0"/>
            <xs:element name="jobTitle" type="xs:string" minOccurs="0"/>
            <xs:element name="id" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

...和wsdl ......

<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. --><definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://recruiter/svc/hr/api/ws" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://recruiter/svc/hr/api/ws" name="EmployeeSvc">
<types>
<xsd:schema>
<xsd:import namespace="http://recruiter/svc/hr/api/ws" schemaLocation="http://recruitment.biz:7001/technology/EmployeeSvc?xsd=1"/>
</xsd:schema>
</types>
<message name="getEmployee">
<part name="parameters" element="tns:getEmployee"/>
</message>
<message name="getEmployeeResponse">
<part name="parameters" element="tns:getEmployeeResponse"/>
</message>
<portType name="EmployeeService">
<operation name="getEmployee">
<input message="tns:getEmployee"/>
<output message="tns:getEmployeeResponse"/>
</operation>
</portType>
<binding name="EmployeeServicePortBinding" type="tns:EmployeeService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getEmployee">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="EmployeeSvc">
<port name="EmployeeServicePort" binding="tns:EmployeeServicePortBinding">
<soap:address location="http://recruitment.biz:7001/technology/EmployeeSvc"/>
</port>
</service>
</definitions>

我尝试了[无效]以下SO线程建议的一些事情......

  1. How to marshall a string using JAXB that sometimes contains XML content and sometimes does not? [谈论一个已知的错误 JAXB-RI ]
  2. unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes
  3. Marshaling exception when using jaxb
  4. 啧!期望能够编组JDK提供的类型是不是太过分了?我的意思是......真的吗?

    拜托,有人可以帮忙吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

尤里卡!

最后我发现需要将deduper.App和deduper.RecruiterWSClient更改为此...

package deduper;

import recruiter.svc.hr.*;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;


public class App { 

   public static void main( String [] args ) { 

   ApplicationContext ctx = SpringApplication.run( RecruiterWSConfiguration.class, args );

   RecruiterWSClient recrWSClient = ctx.getBean( RecruiterWSClient.class );
   GetEmployee empReq = new ObjectFactory( ).createGetEmployee( );
   empReq.setId( 6345789 );
   GetEmployeeResponse empResp = recrWSClient.getEmployee( empReq );
   Employee emp = empResp.getEmployee( );

   System.out.println( "[emp id = " + emp.getId( ) + " : job title = " + emp.getJobTitle( ) + " : emp name = " + emp.getEmployeeName( ) + "]" );

   }
}

......再次......

package deduper;

import recruiter.svc.hr.*;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;

public class RecruiterWSClient extends WebServiceGatewaySupport{ 

   public RecruiterWSClient( ) { }


   public GetEmployeeResponse getEmployee( GetEmployee empReq ) { 

      return (GetEmployeeResponse)getWebServiceTemplate( ).marshalSendAndReceive( empReq );

   }

}

...... Et viola!通过上述更改,它就像一个好员工应该:)