【问题标题】:I get the 'Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource' for my spring boot application我的 Spring Boot 应用程序出现“在类路径资源中定义名称为 'requestMappingHandlerAdapter' 的 bean 创建错误”
【发布时间】:2020-06-17 00:40:22
【问题描述】:

我很难用 spring 来启动我的服务器。我查找了与我类似的错误,这似乎与我使用 RequestMapping 的方式有关。尽管尝试了各种方法来定义我的路线,但我仍然无法弄清楚似乎是什么问题。有什么想法吗?

这是我的错误信息:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qualificationRepository' defined in com.salay.christophersalayportfolio.repositories.QualificationRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.salay.christophersalayportfolio.models.ProgrammingLanguage.dissertation in com.salay.christophersalayportfolio.models.Dissertation.programmingLanguages
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qualificationRepository' defined in com.salay.christophersalayportfolio.repositories.QualificationRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.salay.christophersalayportfolio.models.ProgrammingLanguage.dissertation in com.salay.christophersalayportfolio.models.Dissertation.programmingLanguages

我的任务控制器

import com.salay.christophersalayportfolio.models.Task;
import com.salay.christophersalayportfolio.repositories.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(path = "/api/v1/tasks")
public class TaskController {

    @Autowired
    private TaskRepository taskRepository;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody String add(@RequestBody Task task) {
        taskRepository.save(task);
        return "Successfully saved the task to the database.";
    }

    @RequestMapping(value = "/{id/", method = RequestMethod.GET)
    public Task getById(@PathVariable int id){

        Optional<Task> task = taskRepository.findById(id);

        if(!task.isPresent()){
            throw new NullPointerException();
        }
        else {
            return task.get();
        }
    }

    @RequestMapping(value = "/all/", method = RequestMethod.GET)
    public List<Task> getAll() {
        return taskRepository.findAll();
    }

}

我的编程语言控制器:

package com.salay.christophersalayportfolio.controllers;

import com.salay.christophersalayportfolio.models.ProgrammingLanguage;
import com.salay.christophersalayportfolio.repositories.ProgrammingLanguageRepositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(path = "/api/v1/programmingLanguages")
public class ProgrammingLanguageController {

    @Autowired
    private ProgrammingLanguageRepositories plRepository;

    @RequestMapping( value= "/add", method = RequestMethod.POST)
    public @ResponseBody String add(@RequestBody ProgrammingLanguage pl){
        plRepository.save(pl);
        return "This programming language has been successfully saved";
    }


    @RequestMapping( value = "/{id}/", method = RequestMethod.GET)
    public ProgrammingLanguage getById(@PathVariable int id){
        Optional<ProgrammingLanguage> pl = plRepository.findById(id);

        if( !pl.isPresent()){
            throw new NullPointerException();
        }
        else {
            return pl.get();
        }
    }

    @RequestMapping( value = "/all", method = RequestMethod.GET)
    public List<ProgrammingLanguage> getAll(){
        return plRepository.findAll();
    }
}

以下是我的实体:

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

@Entity
public class ProgrammingLanguage {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    public String name;
    public String image;
}
import javax.persistence.*;
import java.util.Set;

@Entity
public class Dissertation {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private int qualificationId;
    public String title;

    @OneToMany(mappedBy="dissertation")
    public Set<ProgrammingLanguage> programmingLanguages;
    public Set<Tool> tools;
}
package com.salay.christophersalayportfolio.models;

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

@Entity
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private int experienceId;
    private int projectId;
    public String description;
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private int experienceId;
    private int projectId;
    public String description;
}
import javax.persistence.*;
import java.util.Set;

@Entity
public class Project {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    public String name;
    public String description;
    public String client;

    @OneToMany(mappedBy = "project")
    public Set<ProgrammingLanguage> programmingLanguages;

    @OneToMany(mappedBy = "project")
    public Set<Tool> tools;

    @OneToMany(mappedBy = "project")
    public Set<String> images;

    @OneToMany(mappedBy = "project")
    public Set<Task> tasks;
}

我的 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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.salay</groupId>
    <artifactId>christopher-salay-portfolio</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>christopher-salay-portfolio</name>
    <description>Back-end application for my portfolio</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>8.3.1.jre14-preview</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 我认为问题出在您的ProgrammingLanguage 模型上。您还需要发布您的模型

标签: java spring spring-boot hibernate spring-mvc


【解决方案1】:

问题不在您的请求映射中,由于实体映射中的问题,您的应用无法启动,请仔细检查您的映射,如错误中所述:ma​​ppedBy reference an unknown target entity

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.salay.christophersalayportfolio.models.ProgrammingLanguage.dissertation in com.salay.christophersalayportfolio.models.Dissertation.programmingLanguages

您的班级 com.salay.christophersalayportfolio.models.ProgrammingLanguage 似乎没有名为 dissertation

的属性

否则,分享你的模型来看看!

【讨论】:

  • ~@arturo.bhn 是对的。我之前遇到过类似的问题,可能是您的论文属性具有@Id 注释,而它不是主键。无论哪种方式,请发布您的双向关联实体。
  • @arturo.bhn ,我已经用实体更新了问题。编程语言没有论文属性。我的许多模型的目的是创建多值属性。数据库本身有一个名为 DissertationProgrammingLangues 的表。
  • 对,您的问题出在此处:@OneToMany(mappedBy="dissertation") 查看此简短阅读的第 4 部分,这将帮助您了解映射的工作原理。 baeldung.com/jpa-joincolumn-vs-mappedby 干杯!
猜你喜欢
  • 2021-11-11
  • 2015-03-18
  • 2019-08-30
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2020-07-27
  • 2020-03-25
  • 1970-01-01
相关资源
最近更新 更多