HTTP status 404 - Not Found while creating springBoot Application
Asked Answered
P

6

11

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

Pomposity answered 2/12, 2017 at 6:47 Comment(5)
can you please post complete server startup log ?Disfranchise
I can't see this line in your console logs: 2017-12-02 12:21:49.266 INFO 18752 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto ..... . Seems like your your requestmapping is not mappedKatharynkathe
click on console log linkPomposity
Hey hi... I just changed SpringBoot version [1.5.9] to [1.4.2] RELEASE.. It's working.. Thanks guys for your efforts..Pomposity
That's weird, because it works fine for me with Spring Boot 1.5.9 and JDK 1.8u91.Yearwood
E
2

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

Ebullient answered 3/6, 2019 at 13:3 Comment(0)
Y
0

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
Yearwood answered 2/12, 2017 at 7:13 Comment(2)
is it the problem of tomcat?Pomposity
No, it's a Spring issue. If you compare your console log with mine, yours skips a lot after the Mapping filter: 'characterEncodingFilter', which is weird. You are running with Java 8, right? I am running with JDK 1.8u91.Yearwood
A
0

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

Apollinaire answered 2/12, 2017 at 7:25 Comment(5)
OP is using @RestController, not @Controller, for a reason, i.e. so return value is the REST payload of the response, not a view name.Yearwood
It is a his mistake too i thoughtApollinaire
OP posted all the code, i.e. a good nice MCVE, so if OP had intended the Hi return value to be a view, then OP would likely have posted such a view. The code in question works fine exactly as it is (at least it does for me), so it's not a code issue, but more likely a build or environment issue.Yearwood
Just look up it again, return method is string not the rest response. Likely, @Controller is what he need. Anyway stop hereApollinaire
String is a valid REST response. I don't need to look anything up, because I already know that the code in the question works fine as it is.Yearwood
H
0

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

Houseyhousey answered 12/5, 2018 at 18:13 Comment(0)
C
0

I think the issue may be in spring boot version. Better to downgrade to version 1.4.2

Carbamidine answered 25/3 at 5:14 Comment(0)
C
-2

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

Carbamidine answered 2/12, 2017 at 6:57 Comment(3)
i just want hi msg display on browserPomposity
yeah i know...spring boot based on servlet concept.Therefore page need to view data...404 page means their were no page found.So create a view page and add my above cord to your controller methord..after that you can view your msg in browser....use ${Hi} for retreive data to the browserCarbamidine
@NipunVidarshana OP uses @RestController and it means there will be no view template resolver involved, it is basically @ResponseBody on every @RequestMapping, so there is no point of having model. Spring will not even try to look for jsp/theamleaf/static file with @RestControllerFollower

© 2022 - 2024 — McMap. All rights reserved.