Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring Boot Application 404 Not Found

I implemented a simple Spring Boot application using the following classes:

Application Class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackageClasses = CustomerController.class)
public class Application {

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

}

Model Class:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Customer {
    private Long id;
    private String Name;
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
}

Controller Class:

@RestController
public class CustomerController {
    
    @RequestMapping(name="/customer", method=RequestMethod.GET)
    public Customer getCustomer(@RequestParam Long id) {
        Customer cust = new Customer();
        cust.setId(id);
        cust.setName("George");
        return cust;
    }
    
    @RequestMapping(name="/customer", method=RequestMethod.POST, consumes = "application/json")
    public Customer createCustomer(@RequestBody Customer cust) {
        return cust;
    }
}

The application was initiated as Spring Boot and produced the following logs:

2022-09-14 13:28:17.448  INFO 13564 --- [           main] Application     : Starting Application using Java 18 on DESKTOP with PID 13564 
2022-09-14 13:28:17.453  INFO 13564 --- [           main] Application     : No active profile set, falling back to 1 default profile: "default"
2022-09-14 13:28:18.757  INFO 13564 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-09-14 13:28:18.773  INFO 13564 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-14 13:28:18.773  INFO 13564 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-14 13:28:18.904  INFO 13564 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-09-14 13:28:18.904  INFO 13564 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1348 ms
2022-09-14 13:28:19.332  INFO 13564 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-14 13:28:19.342  INFO 13564 --- [           main] Application     : Started Application in 2.661 seconds (JVM running for 3.766)
2022-09-14 13:28:27.493  INFO 13564 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-09-14 13:28:27.494  INFO 13564 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-09-14 13:28:27.495  INFO 13564 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

When i start postman invoke the following:

GET http://localhost:8080/customer?id=1

and i get the following error:

{
    "timestamp": "2022-09-14T10:28:27.548+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/customer"
}

>Solution :

Your @RequestMapping annotation should use value="customer" instead of name="customer".

Better practice is to annotate the CustomerController with @RestController and @ReqeustMapping("/customer") and mark the getCustomer method with @GetMapping, and the create Customer with @PostMapping

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading