我使用jaxb2编组器生成类,以便与webservice进行通信。Java类是通过使用一些wsdl文件生成的。
现在一切都好了,但是当我尝试使用一些生成的类时,我得到了这个解组错误,尽管我使用了生成的ObjectFactory类。
一些堆栈:
org.springframework.ws.soap.client.SoapFaultClientException: Unmarshalling Error: unexpected element (uri:"http://xxxxxxxxx", local:"customer"). Expected elements are <{}customer>
at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:38)
at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:826)
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:621)
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 org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:373)
at einvoice.service.CustomerService.createCustomer(CustomerService.java:40)
at einvoice.controller.facturatie.FacturatieOverzichtController.handleRenderRequest(FacturatieOverzichtController.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)我的同学:
@Service
public class CustomerService {
@Autowired
private WebServiceTemplate customerDaoTemplate;
private ObjectFactory customerObjectFactory;
public CustomerService() {
customerObjectFactory = new ObjectFactory();
}
public boolean createCustomer(Customer c)
{
System.out.println("CREATING CUSTOMER");
einvoice.proxy.customerdaoservice.Customer customer = customerObjectFactory.createCustomer();
customer.setConnectionURL("test");
customer.setUid("testuid");
customer.setName("KorneelTest");
Create create = customerObjectFactory.createCreate();
create.setCustomer(customer);
try
{
customerDaoTemplate.marshalSendAndReceive(customerObjectFactory.createCreate(create));
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}显示此错误的是createCustomer()方法。
The generated ObjectFactory class:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2014.09.12 at 12:04:04 PM CEST
//
package einvoice.proxy.customerdaoservice;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the be.icredit.einvoice.proxy.customerdaoservice package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _AbstractFilter_QNAME = new QName("http://xxxxxxx/", "abstractFilter");
private final static QName _Customer_QNAME = new QName("http://xxxxxxx/", "customer");
private final static QName _FindSingleResponse_QNAME = new QName("http://xxxxxxx/", "findSingleResponse");
private final static QName _Create_QNAME = new QName("http://xxxxxxx/", "create");
private final static QName _Find_QNAME = new QName("http://xxxxxxx/", "find");
private final static QName _CreateResponse_QNAME = new QName("xxxxxxx/", "createResponse");
private final static QName _FindResponse_QNAME = new QName("http://xxxxxxx/", "findResponse");
private final static QName _UpdateResponse_QNAME = new QName("http://xxxxxxx/", "updateResponse");
private final static QName _FindSingle_QNAME = new QName("http://xxxxxxx/", "findSingle");
private final static QName _CustomerFilter_QNAME = new QName("http://xxxxxxx/", "customerFilter");
private final static QName _Update_QNAME = new QName("http://xxxxxxx/", "update");
private final static QName _AbstractEntity_QNAME = new QName("http://xxxxxxx/", "abstractEntity");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: be.icredit.einvoice.proxy.customerdaoservice
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Update }
*
*/
public Update createUpdate() {
return new Update();
}
/**
* Create an instance of {@link CustomerFilter }
*
*/
public CustomerFilter createCustomerFilter() {
return new CustomerFilter();
}
/**
* Create an instance of {@link UpdateResponse }
*
*/
public UpdateResponse createUpdateResponse() {
return new UpdateResponse();
}
/**
* Create an instance of {@link FindSingle }
*
*/
public FindSingle createFindSingle() {
return new FindSingle();
}
/**
* Create an instance of {@link FindResponse }
*
*/
public FindResponse createFindResponse() {
return new FindResponse();
}
/**
* Create an instance of {@link CreateResponse }
*
*/
public CreateResponse createCreateResponse() {
return new CreateResponse();
}
/**
* Create an instance of {@link FindSingleResponse }
*
*/
public FindSingleResponse createFindSingleResponse() {
return new FindSingleResponse();
}
/**
* Create an instance of {@link Customer }
*
*/
public Customer createCustomer() {
return new Customer();
}
/**
* Create an instance of {@link Create }
*
*/
public Create createCreate() {
return new Create();
}
/**
* Create an instance of {@link Find }
*
*/
public Find createFind() {
return new Find();
}
/**
* Create an instance of {@link Pager }
*
*/
public Pager createPager() {
return new Pager();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link AbstractFilter }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "abstractFilter")
public JAXBElement<AbstractFilter> createAbstractFilter(AbstractFilter value) {
return new JAXBElement<AbstractFilter>(_AbstractFilter_QNAME, AbstractFilter.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Customer }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "customer")
public JAXBElement<Customer> createCustomer(Customer value) {
return new JAXBElement<Customer>(_Customer_QNAME, Customer.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link FindSingleResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "findSingleResponse")
public JAXBElement<FindSingleResponse> createFindSingleResponse(FindSingleResponse value) {
return new JAXBElement<FindSingleResponse>(_FindSingleResponse_QNAME, FindSingleResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Create }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "create")
public JAXBElement<Create> createCreate(Create value) {
return new JAXBElement<Create>(_Create_QNAME, Create.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Find }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "find")
public JAXBElement<Find> createFind(Find value) {
return new JAXBElement<Find>(_Find_QNAME, Find.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link CreateResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "createResponse")
public JAXBElement<CreateResponse> createCreateResponse(CreateResponse value) {
return new JAXBElement<CreateResponse>(_CreateResponse_QNAME, CreateResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link FindResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "findResponse")
public JAXBElement<FindResponse> createFindResponse(FindResponse value) {
return new JAXBElement<FindResponse>(_FindResponse_QNAME, FindResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link UpdateResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "updateResponse")
public JAXBElement<UpdateResponse> createUpdateResponse(UpdateResponse value) {
return new JAXBElement<UpdateResponse>(_UpdateResponse_QNAME, UpdateResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link FindSingle }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "findSingle")
public JAXBElement<FindSingle> createFindSingle(FindSingle value) {
return new JAXBElement<FindSingle>(_FindSingle_QNAME, FindSingle.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link CustomerFilter }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "customerFilter")
public JAXBElement<CustomerFilter> createCustomerFilter(CustomerFilter value) {
return new JAXBElement<CustomerFilter>(_CustomerFilter_QNAME, CustomerFilter.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Update }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "update")
public JAXBElement<Update> createUpdate(Update value) {
return new JAXBElement<Update>(_Update_QNAME, Update.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link AbstractEntity }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "abstractEntity")
public JAXBElement<AbstractEntity> createAbstractEntity(AbstractEntity value) {
return new JAXBElement<AbstractEntity>(_AbstractEntity_QNAME, AbstractEntity.class, null, value);
}
}包-info.java类:
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2014.09.12 at 03:44:51 PM CEST
//
@javax.xml.bind.annotation.XmlSchema(namespace = "http://xxxxxxx/")
package einvoice.proxy.customerdaoservice;发布于 2014-09-12 21:43:00
package-info.java请张贴您的ObjectFactory和package-info.java (必须在ObjectFactory旁边生成)。
实际上,您不必通过ObjectFactory创建实例,普通的new构造可以正常工作。) ObjectFactory没有魔法。它主要由JAXB在解编组过程中使用。
ps。请在maven-jaxb2-plugin-related问题上加上maven-jaxb2-plugin标签--这会更快地引起我的注意。
发布于 2016-02-01 04:54:39
请使用以下注释
@XmlRootElement(name="Customer")在类的根处,如下所示。由于JAXb不会自动创建@XmlRootElement注释,所以需要手动将该注释放在类的顶部。
The generated ObjectFactory class:
This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2014.09.12 at 12:04:04 PM CEST
//
package einvoice.proxy.customerdaoservice;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the be.icredit.einvoice.proxy.customerdaoservice package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRootElement(name="Customer")
@XmlRegistry
public class ObjectFactory {
private final static QName _AbstractFilter_QNAME = new QName("http://xxxxxxx/", "abstractFilter");
private final static QName _Customer_QNAME = new QName("http://xxxxxxx/", "customer");
private final static QName _FindSingleResponse_QNAME = new QName("http://xxxxxxx/", "findSingleResponse");
private final static QName _Create_QNAME = new QName("http://xxxxxxx/", "create");
private final static QName _Find_QNAME = new QName("http://xxxxxxx/", "find");
private final static QName _CreateResponse_QNAME = new QName("xxxxxxx/", "createResponse");
private final static QName _FindResponse_QNAME = new QName("http://xxxxxxx/", "findResponse");
private final static QName _UpdateResponse_QNAME = new QName("http://xxxxxxx/", "updateResponse");
private final static QName _FindSingle_QNAME = new QName("http://xxxxxxx/", "findSingle");
private final static QName _CustomerFilter_QNAME = new QName("http://xxxxxxx/", "customerFilter");
private final static QName _Update_QNAME = new QName("http://xxxxxxx/", "update");
private final static QName _AbstractEntity_QNAME = new QName("http://xxxxxxx/", "abstractEntity");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: be.icredit.einvoice.proxy.customerdaoservice
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Update }
*
*/
public Update createUpdate() {
return new Update();
}
/**
* Create an instance of {@link CustomerFilter }
*
*/
public CustomerFilter createCustomerFilter() {
return new CustomerFilter();
}
/**
* Create an instance of {@link UpdateResponse }
*
*/
public UpdateResponse createUpdateResponse() {
return new UpdateResponse();
}
/**
* Create an instance of {@link FindSingle }
*
*/
public FindSingle createFindSingle() {
return new FindSingle();
}
/**
* Create an instance of {@link FindResponse }
*
*/
public FindResponse createFindResponse() {
return new FindResponse();
}
/**
* Create an instance of {@link CreateResponse }
*
*/
public CreateResponse createCreateResponse() {
return new CreateResponse();
}
/**
* Create an instance of {@link FindSingleResponse }
*
*/
public FindSingleResponse createFindSingleResponse() {
return new FindSingleResponse();
}
/**
* Create an instance of {@link Customer }
*
*/
public Customer createCustomer() {
return new Customer();
}
/**
* Create an instance of {@link Create }
*
*/
public Create createCreate() {
return new Create();
}
/**
* Create an instance of {@link Find }
*
*/
public Find createFind() {
return new Find();
}
/**
* Create an instance of {@link Pager }
*
*/
public Pager createPager() {
return new Pager();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link AbstractFilter }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "abstractFilter")
public JAXBElement<AbstractFilter> createAbstractFilter(AbstractFilter value) {
return new JAXBElement<AbstractFilter>(_AbstractFilter_QNAME, AbstractFilter.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Customer }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "customer")
public JAXBElement<Customer> createCustomer(Customer value) {
return new JAXBElement<Customer>(_Customer_QNAME, Customer.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link FindSingleResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "findSingleResponse")
public JAXBElement<FindSingleResponse> createFindSingleResponse(FindSingleResponse value) {
return new JAXBElement<FindSingleResponse>(_FindSingleResponse_QNAME, FindSingleResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Create }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "create")
public JAXBElement<Create> createCreate(Create value) {
return new JAXBElement<Create>(_Create_QNAME, Create.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Find }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "find")
public JAXBElement<Find> createFind(Find value) {
return new JAXBElement<Find>(_Find_QNAME, Find.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link CreateResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "createResponse")
public JAXBElement<CreateResponse> createCreateResponse(CreateResponse value) {
return new JAXBElement<CreateResponse>(_CreateResponse_QNAME, CreateResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link FindResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "findResponse")
public JAXBElement<FindResponse> createFindResponse(FindResponse value) {
return new JAXBElement<FindResponse>(_FindResponse_QNAME, FindResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link UpdateResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "updateResponse")
public JAXBElement<UpdateResponse> createUpdateResponse(UpdateResponse value) {
return new JAXBElement<UpdateResponse>(_UpdateResponse_QNAME, UpdateResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link FindSingle }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "findSingle")
public JAXBElement<FindSingle> createFindSingle(FindSingle value) {
return new JAXBElement<FindSingle>(_FindSingle_QNAME, FindSingle.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link CustomerFilter }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "customerFilter")
public JAXBElement<CustomerFilter> createCustomerFilter(CustomerFilter value) {
return new JAXBElement<CustomerFilter>(_CustomerFilter_QNAME, CustomerFilter.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Update }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "update")
public JAXBElement<Update> createUpdate(Update value) {
return new JAXBElement<Update>(_Update_QNAME, Update.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link AbstractEntity }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://xxxxxxx/", name = "abstractEntity")
public JAXBElement<AbstractEntity> createAbstractEntity(AbstractEntity value) {
return new JAXBElement<AbstractEntity>(_AbstractEntity_QNAME, AbstractEntity.class, null, value);
}}
发布于 2018-01-16 18:43:03
如果项目中有多个模块,请检查重复的类名或目录名。
我得到了同样的错误,我能够通过更改我的项目中的包名来解决它。
我的项目中有两个模块具有相同的目录结构module/src/main/java/com/example/directory/path。
我们有两个不同的端点和两个不同的WSDL,但它们都来自同一个客户机。当我们从WSDL自动生成POJO类时,它为我们创建了文件结构。我注意到,在XML中生成的命名空间对两个模块中生成的XML使用相同的命名空间,而它们应该是不同的(因此产生了堆栈跟踪)。
为了解决这个问题,我所做的就是将目录名更改为更具体的每个模块。
https://stackoverflow.com/questions/25805166
复制相似问题