gpt4 book ai didi

java - 无法将类型 "java.lang.Integer"编码(marshal)为元素,因为它缺少 @XmlRootElement 注释

转载 作者:行者123 更新时间:2023-12-01 11:21:03 30 4
gpt4 key购买 nike

我的设置...

<小时/>
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( ) + "]" );

}
}
<小时/>

...以及一些 spring 配置的来源...

<小时/>
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

谢啦!对能够编码 JDK 提供的类型的期望是否过高?我的意思是……真的吗?

提前致谢。

最佳答案

Eureka !

我终于想到我需要将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 );

}

}
<小时/>

...等中提琴!通过上述更改,它就像一个好员工应该做的那样:)

关于java - 无法将类型 "java.lang.Integer"编码(marshal)为元素,因为它缺少 @XmlRootElement 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226400/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com