【问题标题】:Spring Data REST endpoints gives 404 status codeSpring Data REST 端点提供 404 状态码
【发布时间】:2020-02-17 17:19:36
【问题描述】:

我正在尝试开发一个简单的 Spring Boot Customer CRUD 应用程序。我正在使用 Spring REST Data 和 JPA。 CRUD 的端点应该是自动生成的,但是当我尝试访问它们时,它给出了 404 未找到。

这里是 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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rest.data</groupId>
    <artifactId>demo-application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-application</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

客户实体:

package com.rest.data.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 = "customers")
public class Customer {

    @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;

    public Customer() {

    }

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

    public Customer(int id, String firstName, String lastName, String email) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    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;
    }

}

客户存储库:

package com.rest.data.repository;

import com.rest.data.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;


public interface CustomerRepository extends JpaRepository<Customer, Integer> {

}

和application.properities:

#
##Database properties
#
spring.datasource.url=jdbc:mysql://localhost:3306/customer_crm
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#
##JPA properties 
#
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

#
##Application actuator info data
#
info.app.name=demo-application
info.app.version=1.0.0
info.app.authoer=Mafit Atyo

#
##Expose all endpoints for the actuator
#
management.endpoints.web.exposure.include=*


#
##Application context path
#
server.servlet.context-path=/demo-application

我正在尝试访问 http://localhost:8080/demo-application/customers 以将所有客户列为 JSON,但它返回一个状态为 404 的白标签页面。有什么建议吗?

【问题讨论】:

  • 启动spring-boot时有什么错误吗?
  • 否,执行器端点工作正常。
  • Application的主类组件是否扫描com.rest.data.repository或com.rest.data包?
  • 这是一个spring boot应用,会自动扫描父包的子包。
  • 请粘贴您的主应用程序类的内容

标签: spring spring-boot spring-data-jpa spring-data-rest


【解决方案1】:

从示例中试试这个 (https://spring.io/guides/gs/accessing-data-rest/):

package com.example.accessingdatarest;

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

  List<Person> findByLastName(@Param("name") String name);

}

【讨论】:

【解决方案2】:

我发现了问题! Spring Boot 应用程序无法扫描和检测存储库和实体,因为它们不是主包中的子包。

我在com.rest.data.entitycom.rest.data.repository 中有Customer 实体和CustomerRepository,它们不是主包com.rest.data.demoapplication 的子包。因此,应用程序无法扫描和检测它们,但是当我将实体和存储库包的名称更改为图片时,它就起作用了!

【讨论】:

    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 2016-07-11
    • 2019-12-27
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    相关资源
    最近更新 更多