添加依赖项后 Spring 数据 REST 出现问题
Issue with Spring Data REST after adding dependency
我正在尝试使用 spring Data REST 配置我的 spring 启动应用程序。我在使用 Spring-Data REST 显示来自我的实体 class 的数据时遇到了挑战。我已将必要的依赖项添加到我的 pom.xml 文件中,但是当我尝试在网络浏览器上或通过邮递员访问端点时,我什么也没得到。
查看来自网络浏览器和邮递员的错误消息:
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>
这是我的实体 class,名称为 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> {
}
如有任何帮助,我将不胜感激。谢谢
编辑:此答案不适用于 Spring 数据 REST 用户,因为显然它会自动配置映射。
我在您发布的代码中没有看到 /api/employees 的映射。
您是否定义了一个带有 @GetMapping("/api/employees")
注释的方法?
它应该在用 @RestController
.
注释的 class 中
@GetMapping("/api/employees")
public List<Employee> getEmployees() {
// Get employees from EmployeeRepository
}
我通过更改我的 application.properties 文件中的基本 uri 解决了这个问题,我添加了 spring.data.rest.basePath=/api
并且它像魔术一样工作
根据 Spring Data Rest 的文档,“默认情况下,Spring Data REST 在根 URI“/”处提供 REST 资源。有多种方法可以更改基础路径。
使用 Spring Boot 1.2 及更高版本,您可以通过在 application.properties 中设置单个 属性 来更改基本 URI,如下所示:spring.data.rest.basePath=/api
"
有关 Spring Data REST 的更多详细信息,请使用此 link:Spring Data REST reference guide
我正在尝试使用 spring Data REST 配置我的 spring 启动应用程序。我在使用 Spring-Data REST 显示来自我的实体 class 的数据时遇到了挑战。我已将必要的依赖项添加到我的 pom.xml 文件中,但是当我尝试在网络浏览器上或通过邮递员访问端点时,我什么也没得到。
查看来自网络浏览器和邮递员的错误消息:
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>
这是我的实体 class,名称为 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> {
}
如有任何帮助,我将不胜感激。谢谢
编辑:此答案不适用于 Spring 数据 REST 用户,因为显然它会自动配置映射。
我在您发布的代码中没有看到 /api/employees 的映射。
您是否定义了一个带有 @GetMapping("/api/employees")
注释的方法?
它应该在用 @RestController
.
@GetMapping("/api/employees")
public List<Employee> getEmployees() {
// Get employees from EmployeeRepository
}
我通过更改我的 application.properties 文件中的基本 uri 解决了这个问题,我添加了 spring.data.rest.basePath=/api
并且它像魔术一样工作
根据 Spring Data Rest 的文档,“默认情况下,Spring Data REST 在根 URI“/”处提供 REST 资源。有多种方法可以更改基础路径。
使用 Spring Boot 1.2 及更高版本,您可以通过在 application.properties 中设置单个 属性 来更改基本 URI,如下所示:spring.data.rest.basePath=/api
"
有关 Spring Data REST 的更多详细信息,请使用此 link:Spring Data REST reference guide