JAXBException:类上不是有效的属性

2022-09-04 04:07:41

我们有一个应用程序需要使用外部Web服务。为此,我们使用 cxf-codegen-plugin 插件提供的 wsdl2java 目标,通过 Maven 从 WSDL 生成了一组 Java 工件。

在应用程序中,我们希望设置用于运行时 Web 服务调用的终结点(以满足测试环境中不同的 Web 服务终结点 URL),因此编写了一些代码,如下所示为我们执行此操作:

private <T> T createServiceObject(final Class<T> p_seiClass) throws MalformedURLException {

        final Service serviceFactory = Service.create(new URL(wsdlLocation), new QName(targetNamespace, serviceName));
        final T service = serviceFactory.getPort(p_seiClass);
        ((BindingProvider) service).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "endpoint");

        return service;
    }

当代码运行时,它在 serviceFactory.getPort 行上失败,并使用以下执行:

javax.xml.ws.WebServiceException: class ZZZ.YYYwebservice.v5.types.ProcessUIRequestResponse do not have a property of the name ProcessUIRequestResult
    at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.<init>(ResponseBuilder.java:512)
    at com.sun.xml.internal.ws.client.sei.SEIMethodHandler.buildResponseBuilder(SEIMethodHandler.java:172)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.<init>(SyncMethodHandler.java:86)
    at com.sun.xml.internal.ws.client.sei.SEIStub.<init>(SEIStub.java:83)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBaseProxy(WSServiceDelegate.java:641)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:344)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:326)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:364)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:368)
    at javax.xml.ws.Service.getPort(Service.java:172)
    at com.XXX.XXX.XXX.YYY.integration.facade.jaxws.ProcessUIRequestFacadeJaxws.createServiceObject(ProcessUIRequestFacadeJaxws.java:53)
    at com.XXX.XXX.XXX.YYY.integration.facade.jaxws.ProcessUIRequestFacadeJaxws.processUIRequest(ProcessUIRequestFacadeJaxws.java:39)
    at com.XXX.XXX.XXX.YYY.integration.facade.jaxws.ProcessUIRequestFacadeJaxwsTest.test(ProcessUIRequestFacadeJaxwsTest.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.xml.bind.JAXBException: ProcessUIRequestResult is not a valid property on class ZZZ.YYYwebservice.v5.types.ProcessUIRequestResponse
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getElementPropertyAccessor(JAXBContextImpl.java:954)
    at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.<init>(ResponseBuilder.java:501)
    ... 37 more

在底部,我可以看到javax.xml.bind.JAXBException:ProcessUIRequestResult不是类ZZZ上的有效属性。YYYwebservice.v5.types.ProcessUIRequestResponse 表明生成的 ProcessUIRequestResponse.java文件有问题。

我已经查看了该文件,看不到任何明显的东西(ProcessUIRequestResult在类中定义,并且具有getter和setter)。该类如下:

package YYY.ZZZwebservice.v5.types;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import YYY.ZZZbase.BaseVO;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="ProcessUIRequestResult" type="{http://YYY/ZZZbase/}BaseVO" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "processUIRequestResult"
})
@XmlRootElement(name = "ProcessUIRequestResponse")
public class ProcessUIRequestResponse {

    @XmlElement(name = "ProcessUIRequestResult")
    protected BaseVO processUIRequestResult;

    /**
     * Gets the value of the processUIRequestResult property.
     * 
     * @return
     *     possible object is
     *     {@link BaseVO }
     *     
     */
    public BaseVO getProcessUIRequestResult() {
        return processUIRequestResult;
    }

    /**
     * Sets the value of the processUIRequestResult property.
     * 
     * @param value
     *     allowed object is
     *     {@link BaseVO }
     *     
     */
    public void setProcessUIRequestResult(BaseVO value) {
        this.processUIRequestResult = value;
    }

    public ProcessUIRequestResponse withProcessUIRequestResult(BaseVO value) {
        setProcessUIRequestResult(value);
        return this;
    }

}

有没有人知道问题可能是什么?我无法发现任何明显的东西,也无法找到有关问题可能是什么的任何信息。

提前感谢您的帮助。

编辑 - 根据洛伦佐的回答更新20/09

不幸的是,Web服务是第三方服务,我无权修改XSD。在从 WSDL 生成 Java 时,我使用了 WSDL 的服务器 URL。

但是,作为测试,我修改了生成的 ProcessUIRequestResponse.java将属性更改为以大写字母开头以匹配 WSDL,并抛出了相同的错误。

有人有什么想法吗?

在26/09上进一步编辑以提供更新**

我一直在对此进行更多调查,发现它实际上看起来是服务终结点接口生成方式的问题。这是生成的:

/**
 * This class was generated by Apache CXF 2.5.2 2013-09-26T13:05:17.389+01:00 Generated source version: 2.5.2
 * 
 */
@WebService(targetNamespace = "http://zzz/yyywebservice/v5/types/", name = "Types")
@XmlSeeAlso({ zzz.yyyentityview.ObjectFactory.class, zzz.yyyview.search.postcode.ObjectFactory.class,
        zzz.yyyentityview.validation.ObjectFactory.class, zzz.serializable_dictionary.ObjectFactory.class,
        zzz.yyyview.search.app.ObjectFactory.class, zzz.yyybase.ObjectFactory.class, zzz.yyybase.enums.ObjectFactory.class,
        zzz.yyyview.uw.ObjectFactory.class, zzz.yyyview.app.ObjectFactory.class, zzz.yyyview.search.bank.ObjectFactory.class,
        zzz.yyyview.search.list.ObjectFactory.class, zzz.yyyentityview.app.ObjectFactory.class,
        zzz.yyyentityview.client.ObjectFactory.class, ObjectFactory.class })
public interface Types {

    @WebResult(name = "ProcessUIRequestResult", targetNamespace = "")
    @ResponseWrapper(localName = "ProcessUIRequestResponse", targetNamespace = "http://zzz/yyywebservice/v5/types/", className = "zzz.yyywebservice.v5.types.ProcessUIRequestResponse")
    @RequestWrapper(localName = "ProcessUIRequest", targetNamespace = "http://zzz/yyywebservice/v5/types/", className = "zzz.yyywebservice.v5.types.ProcessUIRequest")
    @WebMethod(operationName = "ProcessUIRequest", action = "http://zzz/yyywebservice/v5/ProcessUIRequest")
    public zzz.yyybase.BaseVO processUIRequest(
            @WebParam(name = "ProcessUIRequest", targetNamespace = "http://zzz/yyywebservice/v5/types/") zzz.yyybase.BaseVO processUIRequest);

    @WebResult(name = "GetActivityStatusEntityResult", targetNamespace = "")
    @ResponseWrapper(localName = "GetActivityStatusEntityResponse", targetNamespace = "http://zzz/yyywebservice/v5/types/", className = "zzz.yyywebservice.v5.types.GetActivityStatusEntityResponse")
    @RequestWrapper(localName = "GetActivityStatusEntity", targetNamespace = "http://zzz/yyywebservice/v5/types/", className = "zzz.yyywebservice.v5.types.GetActivityStatusEntity")
    @WebMethod(operationName = "GetActivityStatusEntity", action = "http://zzz/yyywebservice/v5/GetActivityStatusEntity")
    public zzz.yyybase.ActivityStatusVOBase getActivityStatusEntity(
            @WebParam(name = "ProcessUIRequest", targetNamespace = "http://zzz/yyywebservice/v5/types/") zzz.yyybase.BaseVO processUIRequest);
}

问题在于@WebResult注释和空白目标命名空间。当我手动编辑类并添加我期望的目标命名空间时(基于在XSD中的查找),我可以成功地调用Web服务。

我已经在XSD中查找了任何缺失的命名空间定义或任何其他明显的错误,但我无法发现任何内容。有没有人有任何关于目标命名空间或@WebResult从哪里生成的指针?


答案 1

只需添加软件包信息.java如下所示:

套餐信息.java :

@XmlSchema(
   namespace = "http://www.eijux.com/webservices/",
   elementFormDefault = XmlNsForm.QUALIFIED
)
package com.starlims.webservices;

答案 2

这可能是wsdl2java如何命名包的问题。如果 targetNamespace (wsdl) 采用驼峰大小写表示法,则您的包也需要采用驼峰大小写。例如:

对于目标命名空间=“http://your.domain/WebAdminServices/WebSiteMgmt”

package domain.your.webAdminServices.webSiteMgmt;-->作品:)

package domain.your.webadminservices.websitemgmt;-->:(不起作用

希望它有帮助,

路易斯


推荐