404 未找到邮递员

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

我运行我的 spring 应用程序,它没有显示任何错误,这是我的控制器

package com.testAOP.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.testAOP.model.Employee;
import com.testAOP.service.EmployeeService;

@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/add/employee")
public Employee addEmployee(@RequestParam("name") String name, @RequestParam("empId") String empId) {
return employeeService.createEmployee(name, empId);
}
}

和我的 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>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>TPSpring5</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>TPSpring5</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-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</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>

所以我的应用程序在端口 8080 上运行,但是当我在邮递员上输入 http://localhost:8080/add/employee?name=test&empId=1 (GET) 时,我得到 404 not found { "时间戳": "2024-05-10T21:59:18.175+00:00", “状态”:404, “错误”:“未找到”, “路径”:“/添加/员工” }

我尝试检查注释,但没有看到任何错误,并且我的控制台显示我的应用程序正在运行


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v3.2.5)[0;39m

[2m2024-05-10T22:57:51.735+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mcom.example.demo.TpSpring5Application   [0;39m [2m:[0;39m Starting TpSpring5Application using Java 17.0.10 with PID 21860 (C:\Users\DELL\Desktop\studies 2A\S4\java api\tp1\TPSpring5\target\classes started by DELL in C:\Users\DELL\Desktop\studies 2A\S4\java api\tp1\TPSpring5)
[2m2024-05-10T22:57:51.737+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mcom.example.demo.TpSpring5Application   [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2024-05-10T22:57:52.357+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[2m2024-05-10T22:57:52.390+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 25 ms. Found 0 JPA repository interfaces.
[2m2024-05-10T22:57:52.900+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8080 (http)
[2m2024-05-10T22:57:52.925+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2024-05-10T22:57:52.925+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine   [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.20]
[2m2024-05-10T22:57:52.995+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2024-05-10T22:57:52.995+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1202 ms
[2m2024-05-10T22:57:53.114+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Starting...
[2m2024-05-10T22:57:53.325+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.pool.HikariPool       [0;39m [2m:[0;39m HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:39fc9a6a-c78c-45d5-853b-97c116ba1079 user=SA
[2m2024-05-10T22:57:53.327+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[2m2024-05-10T22:57:53.372+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[2m2024-05-10T22:57:53.418+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36morg.hibernate.Version                   [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 6.4.4.Final
[2m2024-05-10T22:57:53.454+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.h.c.internal.RegionFactoryInitiator   [0;39m [2m:[0;39m HHH000026: Second-level cache disabled
[2m2024-05-10T22:57:53.677+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.s.o.j.p.SpringPersistenceUnitInfo     [0;39m [2m:[0;39m No LoadTimeWeaver setup: ignoring JPA class transformer
[2m2024-05-10T22:57:54.010+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.h.e.t.j.p.i.JtaPlatformInitiator      [0;39m [2m:[0;39m HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
[2m2024-05-10T22:57:54.016+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2024-05-10T22:57:54.075+01:00[0;39m [33m WARN[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[2m2024-05-10T22:57:54.469+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8080 (http) with context path ''
[2m2024-05-10T22:57:54.482+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [           main][0;39m [2m[0;39m[36mcom.example.demo.TpSpring5Application   [0;39m [2m:[0;39m Started TpSpring5Application in 3.112 seconds (process running for 3.723)
[2m2024-05-10T22:59:10.174+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [nio-8080-exec-3][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[2m2024-05-10T22:59:10.174+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [nio-8080-exec-3][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[2m2024-05-10T22:59:10.183+01:00[0;39m [32m INFO[0;39m [35m21860[0;39m [2m---[0;39m [2m[TPSpring5] [nio-8080-exec-3][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 9 ms

spring eclipse postman spring-aop spring-annotations
1个回答
0
投票

我假设您的主类位于

com.testAOP.main;
但您的控制器类位于
com.testAOP.controller;

使用该包结构,Spring 无法看到您的控制器,因为它没有放置在确切的包中

com.testAOP.main;
为了解决这个问题,您需要将所有类移动到主方法类位置的子包中。

我的疯狂猜测是将控制器从

com.testAOP.controller;
移动到
com.testAOP;
并重新启动您的Spring Boot应用程序。

只要像控制器这样的组件位于主方法包位置的子包中,就可以开始了。

© www.soinside.com 2019 - 2024. All rights reserved.