添加依赖项后Spring Data REST问题

问题描述 投票:0回答:1

我正在尝试使用spring数据REST配置springboot应用程序。我在使用Spring-Data REST显示来自实体类的数据时遇到了挑战。我已经在pom.xml文件中添加了必要的依赖关系,但是当我尝试通过Web浏览器或通过邮递员访问端点时,却什么也没有。

请参阅来自网络浏览器和邮递员的错误消息:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Mar 24 18:41:33 PDT 2020
There was an unexpected error (type=Not Found, status=404).
No message available

Postman error message


{
{
  "timestamp" : "2020-03-25T01:13:38.867+0000",
  "timestamp" : "2020-
  "status" : 404,
  "error" : "Not Found",
  "message" : "No message available",
  "path" : "/api/employees"
}

这里是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.1.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dafe.spring</groupId>
    <artifactId>AppLogger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>AppLogger</name>
    <description>Demo project for Spring Boot</description>

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

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

        <!-- Add dependency for Spring Data REST -->
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

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

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

</project>

这是我的实体类,名称为Employee

package com.dafe.spring.logger.entity;

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

@Entity
@Table(name="employee")
public class Employee {

    // define fields

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;


    // define constructors

    public Employee() {

    }

    public Employee(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    // define getter/setter

    public int getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // define tostring

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }

}


这是我的EmployeeRepository接口

package com.dafe.spring.logger.dao;

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

import com.dafe.spring.logger.entity.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

}

我将不胜感激。谢谢

java spring-boot spring-data-rest
1个回答
0
投票

编辑:此答案不适用于Spring Data REST用户,因为显然它会自动配置映射。

在您发布的代码中看不到/ api / employees的映射。

您是否定义了以@GetMapping("/api/employees")注释的方法?它应该在用@RestController注释的类中。

@GetMapping("/api/employees")
public List<Employee> getEmployees() {
    // Get employees from EmployeeRepository
}
© www.soinside.com 2019 - 2024. All rights reserved.