Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer

I have a problem. I am having this error when I try to run on debug my springboot application but I have the following error

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.app.myapp.MyappApplication]; nested exception is java.lang.IllegalStateException:Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer

My MyappApplication class have the following

package com.app.myapp;
import java.io.File;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Date;

import javax.net.ssl.SSLContext;
import javax.servlet.ServletContext;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.app.myapp.model.Reponse;
import com.app.myapp.service.MyModel;

@SpringBootApplication
public class MyappApplication extends SpringBootServletInitializer implements 
CommandLineRunner {

@Autowired
ServletContext context;

@Autowired private MyModel myModel;
public static void main(String[] args) {
    ApplicationContext acx=SpringApplication.run(MyappApplication.class, args);
    String fileSeparator = File.separator;

}

@Override
public void run(String... args) throws Exception {
    
}
   /*
   @Bean
   public RestTemplate getRestTemplate() {
      return new RestTemplate();
   }*/
    @Bean
    public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
            TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
 
            SSLContext sslContext = SSLContexts.custom()
                            .loadTrustMaterial(null, acceptingTrustStrategy)
                            .build();
 
            SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
 
            CloseableHttpClient httpClient = HttpClients.custom()
                            .setSSLSocketFactory(csf)
                            .build();
 
            HttpComponentsClientHttpRequestFactory requestFactory =
                            new HttpComponentsClientHttpRequestFactory();
 
            requestFactory.setHttpClient(httpClient);
            RestTemplate restTemplate = new RestTemplate(requestFactory);
            return restTemplate;
    }      
 }

My POM is the following:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.1</version>
    <relativePath /> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.app.myapp</groupId>
   <artifactId>myapp</artifactId>
   <version>0.0.1</version>
  <name>myApp</name>
  <description>Building software</description>
   <properties>
    <java.version>1.8</java.version>
   </properties>
   <dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
         <!--           <scope>runtime</scope>-->
        <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
         <!--           <scope>provided</scope>-->
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>
   <dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
  </dependency>

    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<packaging>war</packaging>

What could be the problem?

like image 247
user3841581 Avatar asked May 24 '26 08:05

user3841581


2 Answers

only Invalidating cache did not help for me. Please once check the scope for your tomcat in pom.xml. Sometimes scope is "provided" and that's why it doesn't compile the class and throws this error:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <!--<scope>provided</scope>-->
    </dependency>

If you are commenting any scope here, make sure you restart the system and if needed invalidate cache too for indexes. This worked for me.

like image 185
Shivi Avatar answered May 25 '26 22:05

Shivi


enter image description here

My problem was solved by just invalidating the caches and restarting my IDE

Thanks

like image 38
user3841581 Avatar answered May 25 '26 20:05

user3841581



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!