java.lang.NoSuchFieldError: REFLECTION

32

我正在使用CXF创建一个项目,并使用带有一些安全性的MTOM(我不知道那些信息是否相关)。

当我尝试初始化我的服务器时,收到了以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'example': Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: REFLECTION

这是我的 cxf-servlet.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://cxf.apache.org/jaxws
         http://cxf.apache.org/schemas/jaxws.xsd">
    
    <jaxws:endpoint
                    id="example"
                    address="/example"
                    implementor="br.com.arthur.services.ExampleService">
        
        <!-- Uncomment only if using WS-SecurityPolicy -->
        <jaxws:properties>
            <entry key="mtom-enabled" value="true"/>
            <entry key="ws-security.callback-handler" value-ref="myPasswordCallback"/>
        </jaxws:properties>
        
        <!-- Uncomment only if using standard WSS4J interceptors -->
        <!--jaxws:inInterceptors> <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> 
            <constructor-arg> <map> <entry key="action" value="UsernameToken"/> <entry 
            key="passwordType" value="PasswordText"/> <entry key="passwordCallbackRef" 
            value-ref="myPasswordCallback"/> </map> </constructor-arg> </bean> </jaxws:inInterceptors -->
    </jaxws:endpoint>
    
    <bean id="myPasswordCallback" class="br.com.arthur.services.ServerPasswordCallback"/>
</beans>

这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
 xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <display-name>Sample web service provider</display-name>
    <listener>
        <listener-class>
              org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:META-INF/cxf/cxf.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>WebServicePort</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebServicePort</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>restricted web services</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
</web-app>

这是我的 pom.xml 文件:

<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>br.com.arthur</groupId>
    <artifactId>security-mtom</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>

    <properties>
        <cxf.version>3.0.1</cxf.version>
        <jdk.version>1.7</jdk.version>
        <maven-compiler.version>3.1</maven-compiler.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring.version>4.1.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>2.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-security</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-common-utilities</artifactId>
            <version>2.5.11</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler.version}</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding/>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.basedir}/src/main/java</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${project.basedir}/src/main/resources/wsdl/example.wsdl</wsdl>
                                    <wsdlLocation>classpath:wsdl/example.wsdl</wsdlLocation>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我尝试执行这两个教程http://web-gmazza.rhcloud.com/blog/entry/cxf-usernametoken-profilehttp://thenonsensetechlogs.wordpress.com/2011/05/09/mtom-using-apache-cxf-and-maven/


你的 cxf.xml 文件里有什么内容?我问这个是因为你之前提供了 cxf-servlet.xml,但是在你的 web.xml 文件中引用了 cxf.xml - Ascalonian
4个回答

62

这可能是由于com.sun.xml.bind:jaxb-corecom.sun.xml.bind:jaxb-impl的不同版本引起的。请确保在您的dependency:tree中它们的版本相同。

我在项目中用了-core版本2.2.11和-impl版本2.2.6,导致出现了同样的异常。通过在dependencyManagement部分设置两个版本都为2.2.11,问题得以解决。


1
我正在使用Maven,但在pom.xml中没有更改任何内容,但仍然出现了此错误。我认为这是因为有一次我使用与平时不同的JDK home运行了mvn install,导致不同的-impl和-core jar被安装在/target.../WEB-INF/lib目录中。恢复使用以前的JDK home目录后,我有重复的-impl和-core jar。因此,如果您遇到类似的情况,解决此问题的简单方法是从/target.../WEB-INF/lib中删除错误版本的-impl和-core,或者运行mvn clean install - user435421
在我的情况下,我有几个传递依赖项,这些依赖项引发了起始处提到的异常。为了摆脱它,我使用了 POM-Editor 的“依赖层次结构”页面,并发现 -core-impl 的版本不匹配。排除其中一个后,它开始按预期工作。 - Igor

47
您在 cxf-rt-frontend-jaxws 和 cxf-common-utilities 库之间存在冲突。 这两个库都提供了它们的依赖项com.sun.xml.bind,这就是问题的原因。 因此,您应该从 cxf-common-utilities 中排除 com.sun.xml.bind:jaxb-impl ,或者删除对 cxf-common-utilities 的依赖性。

非常感谢!这对我帮助很大,否则我可能还要再搜索几天... - undefined

2
在运行mvn dependency:tree之后,排除依赖关系。
<exclusions>
  <exclusion>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
  </exclusion>
</exclusions>

或者将依赖项jaxb-impl与jaxb-core库版本同步!这样做会有所帮助,而不是上面的方法。 - Harsh

1

我遇到了同样的错误,后来发现这是jaxb-api和jaxb-impl版本不匹配造成的。在我的pom.xml中,jaxb-api是以provided范围(服务器jboss)包含的,而应用程序则从其他依赖项中选择jaxb-impl。根据堆栈跟踪,它们的版本不同。

Servlet.service() for servlet Spring MVC Servlet threw exception: java.lang.NoSuchFieldError: REFLECTION[0m
    [Server:ODIN64_Server01] [31m             at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.<init>(RuntimeModelBuilder.java:87) [**jaxb-impl-2.1.7.jar:2.1.7**][0m
    .....
    [Server:ODIN64_Server01] [31m             at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:211) **[jboss-jaxb-api_2.2_spec-1.0.4.Final-redhat-3.jar:1.0.4.Final-redhat-3**][0m

因此,在pom中添加jaxb-impl并将范围标记为“provided”确保两者具有相同的版本,从而解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接