Spring boot application fails after start - An attempt was made to call a method that does not exist. The attempt was made from the following location
Asked Answered
D

5

8

I'm new in Spring Boot, I'm trying to build a simple application with REST Api, and I'm getting this error immediately when starting the app:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-24 20:08:19.505 ERROR 11344 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.requestMappingHandlerAdapter(WebMvcAutoConfiguration.java:369)

The following method did not exist:

    'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.requestMappingHandlerAdapter(org.springframework.web.accept.ContentNegotiationManager, org.springframework.format.support.FormattingConversionService, org.springframework.validation.Validator)'

The method's class, org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration, is available from the following locations:

    jar:file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar!/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class

The class hierarchy was loaded from the following locations:

    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport: file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration


Process finished with exit code 0

I did not finish yet with the development of the app, but I can't go on because of this errors.

There are my classes so far:

pom.xml

<?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 https://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.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>micro1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>micro1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
<!--            <version>1.4.200</version>-->
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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-surefire-plugin</artifactId>
                <version>2.19.1</version>
            </plugin>
        </plugins>
    </build>

</project>

controller:

package com.example.micro1.controllers;

import com.example.micro1.entities.User;
import com.example.micro1.services.Service;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
@Data
public class Controller {
    private final Service service;

    @PostMapping
    User addNewUser(@RequestBody User user){
        return service.add(user);
    }
}

service:

package com.example.micro1.services;


import com.example.micro1.entities.User;
import com.example.micro1.repositories.Repository;
import lombok.Data;

@org.springframework.stereotype.Service
@Data
public class Service {
    private final Repository repository;

    public User add(User user){
        return repository.save(user);
    }
}

repository:

package com.example.micro1.repositories;

import com.example.micro1.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;

@org.springframework.stereotype.Repository
public interface Repository extends JpaRepository<User, Integer> {
    @Override
    <S extends User> S save(S s);
}

One entity for now:

package com.example.micro1.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private String phoneNumber;
}

application.properties:

#h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

The application class:

package com.example.micro1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Micro1Application {

    public static void main(String[] args) {
        SpringApplication.run(Micro1Application.class, args);
    }

}

Please tell me if there is something else that I should write here... Thanks!

Discordant answered 24/1, 2021 at 18:20 Comment(0)
C
5

Your starter spring-boot-starter-web contains inside itself these dependencies

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.3.3</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.3.3</version>
  <scope>compile</scope>
</dependency>

besides, you add in your pom.xml that:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.3.RELEASE</version>
    </dependency>

so, when you'll start your application you have in classpath ambiguity. I think it's cause of your problem. Spring Boot starters save you the trouble of specifying explicitly declare dependency. Try to remove spring-webmvc dependency.

Campanula answered 24/1, 2021 at 18:41 Comment(0)
H
1

I had this same issue when I run a Spring Boot Application of Web. But I got following error messages:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.configureHandlerExceptionResolvers(WebMvcAutoConfiguration.java:519)

The following method did not exist:

    org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.addDefaultHandlerExceptionResolvers(Ljava/util/List;)V

The method's class, org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration, is available from the following locations:

    jar:file:/Users/itzhouq/dev/mavne_repository/org/springframework/boot/spring-boot-autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar!/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class

It was loaded from the following location:

    file:/Users/itzhouq/dev/mavne_repository/org/springframework/boot/spring-boot-autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration

This may be spring-boot-autoconfigure's exception, so I have checked the dependency of it:

org.springframework.boot:spring-boot-web:2.2.1.RELEASE-->
org.springframework.boot:spring-boot-starter:2.2.1.RELEASE -->
org.springframework.boot:spring-boot-autoconfigure:2.2.1.RELEASE(omitted for conflict with 2.1.6.RELEASE).

Obviously, the autoconfigure was failing because of a version conflict. To solve this problem, I have added the following dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>

I've confirmed this dependency had been added in the module and I've rerun the application.

Heflin answered 22/2, 2021 at 5:54 Comment(0)
E
1

Even I have got a similar error

An attempt was made to call a method that does not exist. The attempt was made from the following location:

org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.httpSecurity(HttpSecurityConfiguration.java:112)

The following method did not exist:

'void org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.setSecurityContextHolderStrategy(org.springframework.security.core.context.SecurityContextHolderStrategy)'

I checked for the same method setSecurityContextHolderStrategy inside WebAsyncManagerIntegrationFilter class in the Spring Docs site, Its still not updated here link. Then I went into source code of class file WebAsyncManagerIntegrationFilter in Github link and realised that the method was added inside the class since verison 5.8 of Spring.

And when I went to see the Spring Security dependency version in my application it was 5.7.8. So I had to upgrade my Spring security dependency to 5.8 or above to solve this.

Ethic answered 8/6, 2023 at 11:23 Comment(2)
hi, i have a same issue, i've updated to 5.8.6 and in the Class "WebAsyncManagerIntegrationFilter" i dont have a method "setSecurityContextHolderStrategy ", only "doFilterInternal". HttpSecurityConfiguration calls this method in Line 112 How can i fix that, that the "setSecurityContextHolderStrategy " will not be called anymore or something else?Stoichiometric
If you've upgraded , then that should have fixed the issue, Please check by Maven reload of the project once.Ethic
C
0

Add one more dependency is Spring-webMVC because spring web MVC is using for deploying your project

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.3</version>
    <scope>compile</scope>
 </dependency
Creaturely answered 16/8, 2021 at 5:47 Comment(0)
C
0

My error was based on the fact that org.springframework.boot.autoconfigure had problems accessing a library that was located in org.springframework.beans, so I added the following dependency to my pom.xml and the problem was solved.

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>5.3.18</version>
</dependency>
Catenane answered 5/5, 2023 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.