springboot API服务:“status”:404,“error”:未找到

jjhzyzn0  于 2023-08-04  发布在  Spring
关注(0)|答案(1)|浏览(137)

我在使用Spring时遇到了一个非常奇怪的错误,显然我的代码没有为API创建正确的端点。当我打开Web浏览器并尝试访问http://localhost:8081/contacts/retrieveAll的端点时,我总是得到404作为答案。控制台上是这么说的

2023-08-03T15:06:10.176+02:00  INFO 18340 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
2023-08-03T15:06:10.188+02:00 DEBUG 18340 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/contacts/", parameters={}
2023-08-03T15:06:10.194+02:00  WARN 18340 --- [nio-8081-exec-1] o.s.web.servlet.PageNotFound             : No mapping for GET /contacts/
2023-08-03T15:06:10.195+02:00 DEBUG 18340 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND

字符串
在下面的代码中,你可以看到:

  • 端口肯定是8081(Tomcat也在控制台中确认)
  • Controller类定义了我使用的API:/contacts + /retrieveAll(我直接复制了一段已经存在的代码,只是改变了这个类的函数名和一些逻辑)

所以我把我的代码留在这里,希望有人能够发现问题。我整个早上都在测试,我开始想这是一种很难发现的东西,或者是一种非常愚蠢的东西,目前我的眼睛看不见。
实体类:

@Entity
@Table(name = "person")
public class Person {
    
    @Id
    @Column(name = "phone_number")
    private String phoneNumber;
    
    @Column(name = "name_person")
    private String name;
    
    @Column(name = "surname_person")
    private String surname;
    
    @Column(name = "last_modified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastModified;
    
    //No args - JPA's reflection 
    public Person() {
    }
    
    public Person(String phoneNumber, String name, String surname) {
    ...
    }
    
    @PrePersist
    protected void onCreate() {
        lastModified = new Date();
    }

    // getters and setters...
}


联系人类:

public class AddressBook {

    private List<Person> persons;

    public AddressBook() {
        persons = new ArrayList<>();
    }

    public List<Person> getPersons() {
        return persons;
    }

    public void addPerson(Person person) {
        persons.add(person);
    }

    public void removePerson(Person person) {
        persons.remove(person);
    }
}


JpaRepository接口:

@Repository
public interface PersonRepository extends CrudRepository<Person, String> {
   @Override
   Person save(Person person);
   @Override
   @Cacheable("persons")
   List<Person> findAll();
   void deleteByPhoneNumber(String phoneNumber);
   List<Person> searchBySurname(String surname);
   List<Person> searchByPhoneNumber(String phoneNumber);
}


AddressBookService类:

@Service
public class AddressBookService {
    private final AddressBook addressBook;

    public AddressBookService() {
        this.addressBook = new AddressBook();
    }

    public List<Person> retrieveAll() {
        return addressBook.getPersons();
    }

}


AddressBookController类:

@RestController
@RequestMapping("/contacts")
public class AddressBookController {

    @Autowired
    private PersonRepository personRepository;
  
    @GetMapping("/retrieveAll")
    public ResponseEntity<?> retrieveContacts() {
        List<Person> contacts = personRepository.findAll();

        if (contacts.isEmpty()) 
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Nobody found in the address book.");
        
        return ResponseEntity.ok(contacts);
    }


SpringApp

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


POM文件:

<?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>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>it.unicredit</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</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-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!-- Replace version with the appropriate version for your MySQL server -->
            <version>8.0.27</version>
        </dependency>

        
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


属性文件:

spring.datasource.url=jdbc:mysql://localhost:3306/mysql80
spring.datasource.username=root
spring.datasource.password=${DB_PASSWORD}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

server.port=8081
debug=true

rseugnpd

rseugnpd1#

请检查您的应用程序是否托管在上下文路径上。它的地址是**/pimservice**。此路径需要在您托管的任何URI之前预先添加。

2023-08-03T19:26:14.587+05:30  INFO 13644 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8087 (http) with context path '/pimservice'

字符串
从你的问题记录来看

2023-08-03T15:06:10.194+02:00  WARN 18340 --- [nio-8081-exec-1] o.s.web.servlet.PageNotFound             : No mapping for GET /contacts/


路径是/contacts/而不是/contacts/retriveAll。看起来你在客户端上犯了一些错误。

相关问题