19

I have small project in spring boot with oauth2, i want to run resource server

settings:

  • Java 17
  • spring-boot-starter-parent version 2.5.6
  • spring-cloud-dependencies version 2020.0.4
  • spring-boot-starter-oauth2-resource-server
  • spring-security-oauth2-autoconfigure

When i want to run my app i have exception:


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.12.jar:5.3.12]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.12.jar:5.3.12]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.6.jar:2.5.6]
    

Do you have any idea what could be wrong?

kgajda
  • 225
  • 1
  • 2
  • 5
  • 7
    The field `defineClass` is supposed to contain a reference to a `Method` object on which the code tried to invoke the method `invoke(Object, Object...)` but the field is `null`. Typical cause is some `catch(…)` and proceed instead of terminating code which leaves the field uninitialized. If I shall make even more guesses, this code tries to access `Unsafe.defineClass` via Reflection, but [this method does not exist anymore](https://bugs.openjdk.java.net/browse/JDK-8193033), but instead of handling the `NoSuchMethodException` it proceeds and produces a `NullPointerException` at a later point… – Holger Nov 30 '21 at 15:37
  • 2
    In the end, it doesn’t matter to you, as you’re not a Spring developer (I suppose), so if my guess is correct, you have to upgrade the framework (if a newer version exist) or downgrade Java (below 11). – Holger Nov 30 '21 at 15:39
  • @Holger any ideas on this - https://stackoverflow.com/questions/72444442/java-lang-reflect-method-invokeobject-object-exception-trying-to-use-jaxb-o – Paul Taylor May 31 '22 at 08:29

3 Answers3

25

consider add xml lib in dependency, as:

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.messaging.saaj</groupId>
        <artifactId>saaj-impl</artifactId>
        <version>1.5.1</version>
    </dependency>
user
  • 264
  • 3
  • 4
  • 7
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '21 at 09:37
  • 3
    I think the reason this solution worked is that **JAXB was marked for deprecation** in Java 9 and **removed in Java 11**. If the dependencies are not being provided by other libraries (transitive) you have in your project then on Java 17 they cannot be found. That is why you have to add them as direct dependency. [What was removed from 11](https://javaalmanac.io/jdk/11/apidiff/9/) – Vahid Feb 22 '22 at 15:57
8

Just added this dependency, and the error was gone:

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

Thanks to @user

This was tested with:

  • JDK 17 (Eclipse Temurin)
  • Spring Boot 2.6.2
  • Spring Cloud 2021.0.0
  • org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure
vvalenciacruz
  • 341
  • 2
  • 8
2

Upgrading maven-jaxb2-plugin to the version 0.15.1 resolved the issue for me.

 <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>0.15.1</version>
 </plugin>
MA1
  • 926
  • 10
  • 28