【问题标题】:Unable to create JAXBContext creating my wsdl无法创建 JAXBContext 创建我的 wsdl
【发布时间】:2011-11-06 03:14:56
【问题描述】:

我正在尝试为 Web 服务生成我的 WSDL,但我收到此错误:

注:AP轮次:2 线程“主”javax.xml.ws.WebServiceException 中的异常:无法创建 JAXBContext 在 com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:153) 在 com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:83) 在 com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:244) 在 com.sun.tools.internal.ws.wscompile.WsgenTool.buildModel(WsgenTool.java:229) 在 com.sun.tools.internal.ws.wscompile.WsgenTool.run(WsgenTool.java:112) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 com.sun.tools.internal.ws.Invoker.invoke(Invoker.java:105) 在 com.sun.tools.internal.ws.WsGen.main(WsGen.java:41) 引起:java.security.PrivilegedActionException:com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1 个 IllegalAnnotationExceptions 计数 java.lang.StackTraceElement 没有无参数默认构造函数。 此问题与以下位置有关: 在 java.lang.StackTraceElement 在公共 java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace() 在 java.lang.Throwable 在 java.lang.Exception 在 java.sql.SQLException 在私有 java.sql.SQLException wsdb.jaxws.SQLExceptionBean.nextException 在 wsdb.jaxws.SQLExceptionBean 在 java.security.AccessController.doPrivileged(本机方法) 在 com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:140) ... 10 更多 引起:com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1 个 IllegalAnnotationExceptions 计数 java.lang.StackTraceElement 没有无参数默认构造函数。 此问题与以下位置有关: 在 java.lang.StackTraceElement 在公共 java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace() 在 java.lang.Throwable 在 java.lang.Exception 在 java.sql.SQLException 在私有 java.sql.SQLException wsdb.jaxws.SQLExceptionBean.nextException 在 wsdb.jaxws.SQLExceptionBean 在 com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91) 在 com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:436) 在 com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:277) 在 com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100) 在 com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143) 在 com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:95) 在 com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(JAXBContextFactory.java:97) 在 com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:148) 在 com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:140) ... 12 更多

我在互联网上看到了一些关于这个的讨论,首先是为所有类构建一个构造函数,直到创建另一个 xml 规范。事实是,对此没有真正的答案,或者我测试的解决方案不起作用。

我在此处阅读了有关此问题的讨论,但未终止,我不知道如何解决。如果有人对此有所了解,我很感激能将我加入正确的方向以避免这种情况。

我在 JDK 6 和 wsgen 中使用 Debian Squezze、Java 1.6_20、JAX-WS JAX-WS RI 2.1.6 来生成 wsdl。第一步是正确的,生成带有 bean 类的 jaxws 目录。

【问题讨论】:

标签: java web-services jax-ws wsgen


【解决方案1】:

看起来您声明的 API 的一部分包含一个类型,该类型扩展了具有 StackTraceElement[ ] 类型的内部字段的类型。

因为 StackTraceElement 没有零参数构造函数,所以 JAXB 无法反序列化此类的实例。

尝试从 SQLExceptionBean 中删除 nextException 字段,或从您的服务 API 中删除 SQLExceptionBean 类型的参数。

【讨论】:

    【解决方案2】:

    Throwable 对象(即异常和错误)不能直接传输,因为它们不能序列化为 XML(StackTraceElement 没有 JAXB 要求的无参数构造函数)。

    您必须为此使用 SOAP 错误。见this question。它指向您应该放在异常类上的 @WebFault 注释。 (也可能检查thisthis

    【讨论】:

    • 不知道作者,但这对我来说是正确的答案。这让我浪费了半天的时间。
    【解决方案3】:

    众所周知,由于 StackTrace 没有默认构造函数,因此 JAXB 无法编组任何 Exception/Throwable 类。您可以做的是创建您自己的 Exception 类,该类从您的示例 WebServiceException 扩展而来。在您的类中,覆盖将使用 StackTrace 的方法并向它们添加 @XmlTransient 注释。只要您使用 JAXB 2.2.4u2+,Marshaller 就会遵守自定义类中的 @XmlTrasient 注释。

    http://java.net/jira/browse/JAXB-814 提供了更多详细信息,其中概述了 @XmlTransient 在子类上无法正常工作的缺陷。

    下面是一个可以正确编组的示例类:

    package com.stackoverflow.7232331;
    
    import javax.ws.rs.core.Response.Status;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlRootElement
    public class CustomException extends RuntimeException {
    
        /**
         * 
         */
        private static final long serialVersionUID = -672804070691541883L;
        protected String reason;
        protected Status status;
        protected int errorCode;
    
        public String getReason() {
            return reason;
        }
    
        public void setReason(String reason) {
            this.reason = reason;
        }
    
        public Status getStatus() {
            return status;
        }
    
        public void setStatus(Status status) {
            this.status = status;
        }
    
        public int getErrorCode() {
            return errorCode;
        }
    
        public void setErrorCode(int errorCode) {
            this.errorCode = errorCode;
        }
    
        public CustomException(Status status, String message, String reason, int errorCode) {
            super(message);
            this.reason = reason;
            this.status = status;
            this.errorCode = errorCode;
        }
    
        public CustomException() {
            super();
        }
    
        @XmlTransient
        @Override
        public StackTraceElement[] getStackTrace() {
            return super.getStackTrace();
        }
    
        @XmlTransient
        @Override
        public Throwable getCause() {
            return super.getCause();
        }
    
    }
    

    如果您使用 Maven,则需要此依赖项:

    <dependency>
       <groupId>com.sun.xml.bind</groupId>
       <artifactId>jaxb-impl</artifactId>
       <version>2.2.5</version>
    </dependency>
    

    【讨论】:

      【解决方案4】:

      请注意,您不必创建自定义异常。在生成类时,您只需要使用正确版本的 JAXB。例如:

      <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>jaxws-maven-plugin</artifactId>
          <version>1.12</version>
          <dependencies>
              <dependency>
                  <groupId>com.sun.xml.bind</groupId>
                  <artifactId>jaxb-impl</artifactId>
                  <version>2.2.5</version>
              </dependency>
          </dependencies>
          <executions>
              <execution>
                  ...
              </execution>
          </executions>
      </plugin>
      

      【讨论】:

        【解决方案5】:

        Domenic D. 的正确答案对我有用,但我想进一步扩展。

        我的 Maven 项目使用 JDK 6 构建得很好,但是当我切换到使用 JDK 1.7 作为我的默认值时,构建失败了。通过添加显式 jaxb-impl 依赖 w/ 版本,它可以工作,例如:

        <plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.1</version>
            <dependencies>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-impl</artifactId>
                    <version>2.2.6</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                ...
                </execution>
            </executions>
        </plugin>
        

        关于版本:此错误列于https://java.net/jira/browse/JAXB-814。它已针对 2.2.4u2、2.2.5、2.3 版本修复

        【讨论】:

        • 已更新为“com.sun.xml.ws:jaxws-rt:2.2.10”,现在可以了。谢谢!
        【解决方案6】:

        我也有这个问题! 完全一样!

        看到这个错误,

         at java.lang.StackTraceElement
                     at public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace()
                     at java.lang.Throwable
                     at java.lang.Exception
                     at java.sql.SQLException
                     at private java.sql.SQLException wsdb.jaxws.SQLExceptionBean.nextException
                     at wsdb.jaxws.SQLExceptionBean
        

        我检查了我的代码,并检查了原因是

         @WebMethod(operationName = "reportnewplace")
         public String reportnewplace(@WebParam(name = "xcmc") String xcmc, 
                 @WebParam(name = "address") String address, 
                 @WebParam(name = "lon") String lon, 
                 @WebParam(name = "lat") String lat, 
                 @WebParam(name = "UID") String UID) throws SQLException{
        

        web 方法不应该抛出异常,remove 将简单地解决这个问题,如果你坚持这样做,请按照@Bozho 的第一个答案。

        【讨论】:

          【解决方案7】:

          我已通过将 jaxb-impl 版本从 2.1.9 修复到 2.2.6 解决了这个问题 现在可以正常使用了

          <dependency>
                  <groupId>com.sun.xml.bind</groupId>
                  <artifactId>jaxb-impl</artifactId>
                  <version>2.2.6</version>
          </dependency>
          

          【讨论】:

          【解决方案8】:

          我通过在界面顶部添加 SOAPBinding 解决了这个问题:

          public interface xxxWebServiceImpl
          {
              @SOAPBinding(style = SOAPBinding.Style.RPC)
          }
          

          【讨论】:

          • 你在做什么,请澄清你使用'my:'和非常糟糕的语法/英语
          猜你喜欢
          • 1970-01-01
          • 2023-04-06
          • 2018-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-11
          相关资源
          最近更新 更多