Home > Back-end >  Spring Boot Application 404 Not Found
Spring Boot Application 404 Not Found

Time:09-14

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:

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

CodePudding user response:

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

CodePudding user response:

Like example my PostMapping:

 @Controller
 public class MainController {

    @PostMapping("/mainaho")
    public String addAho (
        @RequestParam String owner,
        @RequestParam String text,
        @RequestParam String sn,
        @RequestParam String invid,
        @RequestParam String author, Map<String, Object> model) throws WriterException {
    AddTmcAho addTmcAho = new AddTmcAho();
    addTmcAho.addTmcAho(ahoRepo, owner, text, sn, invid, author, model);
    return "mainaho";
   }
}
  • Related