java.lang.NoSuchFieldError: REFLECTION
Asked Answered
C

4

32

I'm creating a project with CXF and use MTOM with some security (I don't know if that information is relevant).

When I tried initialize my server I received that error:

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

This is my cxf-servlet.xml file:

<?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>

This is my web.xml file:

<?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>

This is my pom.xml file:

<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>

I tried execute those two tutorials http://web-gmazza.rhcloud.com/blog/entry/cxf-usernametoken-profile and http://thenonsensetechlogs.wordpress.com/2011/05/09/mtom-using-apache-cxf-and-maven/

Coma answered 10/9, 2014 at 12:15 Comment(1)
What is in your cxf.xml? I ask because you provided cxf-servlet.xml above, but in your web.xml it refers to cxf.xmlNonrecognition
C
48

You have a conflict between the cxf-rt-frontend-jaxws and cxf-common-utilities libraries. Both of these provide as their dependencies com.sun.xml.bind which is the cause of your problem. So you should either exclude com.sun.xml.bind:jaxb-impl from cxf-common-utilities or remove your dependency on cxf-common-utilities.

Colangelo answered 21/11, 2014 at 17:33 Comment(1)
Thank you very much! This helped me a lot, otherwise I would have searched a few days longer...Zonked
G
63

Or it could be just different versions of com.sun.xml.bind:jaxb-core and com.sun.xml.bind:jaxb-impl. Ensure they are the same across your dependency:tree.

I had -core v.2.2.11 and -impl v.2.2.6 in my project which led to the same exception. After setting both versions to 2.2.11 via dependencyManagement section everything became fine.

Guitar answered 13/5, 2015 at 16:47 Comment(2)
I am using Maven and did not change anything in pom.xml but this error still occured. I think it was because once I ran mvn install with different JDK home than usual different -impl and -core jars got installed in /target.../WEB-INF/lib directory. After resuming to use previous JDK home dir I had dublicate -impl and -core jars. So easy way to fix this if you have similar situation is to delete wrong versions of -impl and -core from /target.../WEB-INF/lib or run mvn clean installViscid
In my case I had several transitive dependencies, which threw the exception mentioned at the start. To get rid of it I used the Dependency Hierarchy page of the POM-Editor and found, that I the version of -core and -impl did not match. After excluding one, it started working as expected.Stunning
C
48

You have a conflict between the cxf-rt-frontend-jaxws and cxf-common-utilities libraries. Both of these provide as their dependencies com.sun.xml.bind which is the cause of your problem. So you should either exclude com.sun.xml.bind:jaxb-impl from cxf-common-utilities or remove your dependency on cxf-common-utilities.

Colangelo answered 21/11, 2014 at 17:33 Comment(1)
Thank you very much! This helped me a lot, otherwise I would have searched a few days longer...Zonked
S
2

Exclude dependence after you run mvn dependency:tree

<exclusions>
  <exclusion>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
  </exclusion>
</exclusions>
Somite answered 29/9, 2020 at 17:36 Comment(1)
Or align the dependency jaxb-impl to sync with jaxb-core library version ! That helped instead of aboveSisley
F
1

Got the same error and found out that in my case there was a mismatch of versions of jaxb-api and jaxb-impl. The jaxb-api was included in the pom.xml with provided scope (server jboss) and the application was picking jaxb-impl from other dependency. These both were of different versions as per the stack trace.

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

So adding jaxb-impl in pom and marking the scope as 'provided' made sure that both has the same version and that fixed the problem.

Favourite answered 21/1, 2020 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.