在Spring Framework 5中发布连接JPA存储库

时间:2018-06-19 05:33:03

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

我最近开始学习Java,以及Spring 5和Spring Boot。我创建网络服务相对较新,所以首先我要创建一个在线宠物商店API。我一直在关注此github教程中的源代码作为指南:https://github.com/in28minutes/spring-boot-examples/tree/master/spring-boot-2-rest-service-with-swagger

要部署API,我运行以下命令:

~mvn clean install
~mvn spring-boot:run

它构建成功,没有错误或警告。但是,当我使用spring-boot:run命令时,我收到以下消息:

  

无法执行目标   org.springframework.boot:弹簧启动了Maven插件:2.0.2.RELEASE:运行   项目gs-rest-service上的(default-cli):发生异常时   运行。 null:InvocationTargetException:创建bean时出错   name' petController':通过字段表达的不满意的依赖性   ' petRepository&#39 ;;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   属于' petstore.pet.PetRepository'的限定bean可供选择:   预计至少有1个豆有资格成为autowire候选人。   依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}    - > [帮助1]

我到处搜索,但我似乎无法找到答案。我的代码如下:

的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <dependencies>
        <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>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
    </dependencies>

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


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

的src /主/爪哇/的PetStore / PetStoreApplication.java

package petstore;

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

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

的src /主/爪哇/的PetStore / PET / Pet.java

package petstore.pet;

import com.sun.javafx.beans.IDProperty;

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

@Entity
public class Pet {

    @Id
    @GeneratedValue
    private long id;

    private String name;
    private String status;

    public Pet(long id, String name, String status)
    {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    public long getId()
    {
        return this.id;
    }

    public String getName()
    {
        return this.name;
    }

    public String getStatus()
    {
        return this.status;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }
}

的src /主/爪哇/的PetStore / PET / PetRepository.java

package petstore.pet;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PetRepository extends JpaRepository<Pet, Long>
{

}

的src /主/爪哇/的PetStore / PetController.java

package petstore.pet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class PetController
{
    @Autowired
    private PetRepository petRepository;

    @GetMapping("/pets")
    public List<Pet>retrieveAllPets()
    {
        return petRepository.findAll();
    }
}

的src /主/爪哇/的PetStore / PET / PetNotFoundException.java

package petstore.pet;

public class PetNotFoundException extends RuntimeException
{
    public PetNotFoundException(String exception)
    {
        super(exception);
    }
}

我相信我错过了布线的东西,但我不确定如何解决它,谷歌搜索也没有让我走得很远。对这个冗长的问题道歉。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

你必须在Spring配置中添加@EnableJpaRepositories(basePackages = ...),因为Spring不知道搜索哪些包以找到你的PetRepository

答案 1 :(得分:0)

可以尝试添加以下依赖项。

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

此外,不需要版本,因为Spring引导具有此自动配置属性,它将自动获取每个依赖项的可行版本。

如果我们“手动”管理依赖项,我们需要指定所有依赖项的版本号。

答案 2 :(得分:0)

您应该只包含以下依赖项以使您的应用程序正常工作。 此外,我添加了h2数据库依赖项,因为您可能需要一个用于应用程序的工作。随意更改您使用过的数据库。

<dependencies>
    <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>
    </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>           
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>