Search code examples
springeclipsepostmanspring-aopspring-annotations

404 not found postman


i run my spring application and it's showing no errors, here's my controller

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

and my 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>

so my app is running on port 8080 , but when i enter http://localhost:8080/add/employee?name=test&empId=1 (GET) on postman , i get 404 not found

{
    "timestamp": "2024-05-10T21:59:18.175+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/add/employee"
}

i tried checking annotations but i dont see anything wrong, and my console shows that my app is running

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.5)

2024-05-10T22:57:51.735+01:00  INFO 21860 --- [TPSpring5] [           main] .example.demo.TpSpring5Application    : 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)
2024-05-10T22:57:51.737+01:00  INFO 21860 --- [TPSpring5] [           main] .example.demo.TpSpring5Application    : No active profile set, falling back to 1 default profile: "default"
2024-05-10T22:57:52.357+01:00  INFO 21860 --- [TPSpring5] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-05-10T22:57:52.390+01:00  INFO 21860 --- [TPSpring5] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 25 ms. Found 0 JPA repository interfaces.
2024-05-10T22:57:52.900+01:00  INFO 21860 --- [TPSpring5] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-05-10T22:57:52.925+01:00  INFO 21860 --- [TPSpring5] [           main] o.apache.catalina.core.StandardService   : Starting service cat]
2024-05-10T22:57:52.925+01:00  INFO 21860 --- [TPSpring5] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.20]
2024-05-10T22:57:52.995+01:00  INFO 21860 --- [TPSpring5] [           main] o.a.c.c.C.cat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-05-10T22:57:52.995+01:00  INFO 21860 --- [TPSpring5] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1202 ms
2024-05-10T22:57:53.114+01:00  INFO 21860 --- [TPSpring5] [           main] .zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-05-10T22:57:53.325+01:00  INFO 21860 --- [TPSpring5] [           main] .zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:39fc9a6a-c78c-45d5-853b-97c116ba1079 user=SA
2024-05-10T22:57:53.327+01:00  INFO 21860 --- [TPSpring5] [           main] .zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-05-10T22:57:53.372+01:00  INFO 21860 --- [TPSpring5] [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo e: default]
2024-05-10T22:57:53.418+01:00  INFO 21860 --- [TPSpring5] [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.4.4.Final
2024-05-10T22:57:53.454+01:00  INFO 21860 --- [TPSpring5] [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled
2024-05-10T22:57:53.677+01:00  INFO 21860 --- [TPSpring5] [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2024-05-10T22:57:54.010+01:00  INFO 21860 --- [TPSpring5] [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2024-05-10T22:57:54.016+01:00  INFO 21860 --- [TPSpring5] [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-05-10T22:57:54.075+01:00  WARN 21860 --- [TPSpring5] [           main] JpaBaseConfiguration$JpaWebConfiguration : 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
2024-05-10T22:57:54.469+01:00  INFO 21860 --- [TPSpring5] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path ''
2024-05-10T22:57:54.482+01:00  INFO 21860 --- [TPSpring5] [           main] .example.demo.TpSpring5Application    : Started TpSpring5Application in 3.112 seconds (process running for 3.723)
2024-05-10T22:59:10.174+01:00  INFO 21860 --- [TPSpring5] [nio-8080-exec-3] o.a.c.c.C.cat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-05-10T22:59:10.174+01:00  INFO 21860 --- [TPSpring5] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-05-10T22:59:10.183+01:00  INFO 21860 --- [TPSpring5] [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms

Solution

  • I assume that your main class is located in com.testAOP.main; but your controller class is in com.testAOP.controller;.

    With that package structure Spring cannot see your controller because it NOT placed in exact package com.testAOP.main; in order to fix that you need to move all your classes into subpackage of your main method class location.

    My wild guess would be to move controller from com.testAOP.controller; to com.testAOP.main; or com.testAOP.main.controller; and restart your spring boot application.

    As long your components like controller are in subpackage of main method package location, you should be good to go.