'HTTP status 404 - Not Found while creating springBoot Application

This a springboot Application. It run perfectly but did not get output (it shows me HTTP Status 404 error in browser)

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.exaample.demo</groupId>
  <artifactId>SpringBootMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven spring boot project</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>   
    </properties>
</project>

Springboot start Class Main Method

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebMainMethod {

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

    }

}

controller is loading after main class

**Rest Controller**


package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHi() {
        return "Hi";
    }
}

Url : http://localhost:8080/hello output

Browser Output

this console log os springboot Application



Solution 1:[1]

Unable to reproduce problem.

This is not an answer, but I copied the 3 files from question, and built and ran code without problem. My console log is however a bit different, included below, and that's why I'm posting this as an answer.

Try doing a clean build. Maybe that will fix the problem.

Console Log

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2017-12-02 02:06:19.763  INFO 13268 --- [           main] com.example.demo.WebMainMethod           : Starting WebMainMethod on XXXX with PID 13268 (C:\Users\XXXX\SpringBootMaven\target\classes started by Andreas in C:\Users\XXXX\SpringBootMaven)
2017-12-02 02:06:19.765  INFO 13268 --- [           main] com.example.demo.WebMainMethod           : No active profile set, falling back to default profiles: default
2017-12-02 02:06:19.794  INFO 13268 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f51b3e0: startup date [Sat Dec 02 02:06:19 EST 2017]; root of context hierarchy
2017-12-02 02:06:20.631  INFO 13268 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-02 02:06:20.641  INFO 13268 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-12-02 02:06:20.642  INFO 13268 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2017-12-02 02:06:20.751  INFO 13268 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-12-02 02:06:20.751  INFO 13268 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 959 ms
2017-12-02 02:06:20.824  INFO 13268 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-02 02:06:20.826  INFO 13268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-02 02:06:20.827  INFO 13268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-02 02:06:20.827  INFO 13268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-02 02:06:20.827  INFO 13268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-02 02:06:21.025  INFO 13268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f51b3e0: startup date [Sat Dec 02 02:06:19 EST 2017]; root of context hierarchy
2017-12-02 02:06:21.071  INFO 13268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.example.demo.controller.HelloController.sayHi()
2017-12-02 02:06:21.074  INFO 13268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-02 02:06:21.074  INFO 13268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-02 02:06:21.095  INFO 13268 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-02 02:06:21.095  INFO 13268 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-02 02:06:21.119  INFO 13268 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-02 02:06:21.195  INFO 13268 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-12-02 02:06:21.271  INFO 13268 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-02 02:06:21.273  INFO 13268 --- [           main] com.example.demo.WebMainMethod           : Started WebMainMethod in 1.908 seconds (JVM running for 2.231)
2017-12-02 02:06:38.426  INFO 13268 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-12-02 02:06:38.426  INFO 13268 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-12-02 02:06:38.435  INFO 13268 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 9 ms

Solution 2:[2]

Maybe your template doesn't exist. Please specify file name of template which map with the retured value. Check this example

@Controller
public class HelloWorldController {
    @RequestMapping("/hello")
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {

        String message="You just create Spring Boot Example successfully";
        model.addAttribute("name", name);
        model.addAttribute("message", message);

        return "hello";
    }
}

Found this from Spring Boot Maven Example Hello World

Solution 3:[3]

It's seems there is some problem at your port 8080.

please change port and try to restart your appliaction.

Example: Add below to your application.properties file

server.port = 8090

and then try hitting http://localhost:8090/hello

Solution 4:[4]

You can try this. Using application.properties /yml

The most straightforward way of changing the context path is to set the property in the application.properties: server.servlet.context-path=/demoApp

Instead of putting the properties file in src/main/resources, we can also keep it in the current working directory (outside of the classpath). Java System Property.

http://localhost:8090/demoApp/hello for more info. https://www.baeldung.com/spring-boot-context-path

Solution 5:[5]

Do you create a jsp or html page called hi... Check your views... You don;t have page to view.... Please create a jsp and put it on

   @RequestMapping("/hello")
        public String sayHi(Model model) {
        model.addAttribute("Hi","Hi")            
       return "Hi";
        }

jsp page must be Hi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andreas
Solution 2 David Pham
Solution 3 wandermonk
Solution 4 Fany Gómez
Solution 5 Nipun Vidarshana