Harshit
Harshit

Reputation: 5157

Spring Boot 404 not found error

I am trying to run the spring boot application but getting the 404 not found error.

Project structure:

src/
 +- main/
     +- java/
     |   + com/
         |   + demo/
             |    SpringBootDemo.java
             |    + controller/
                  |    HomeController.java
     +- resources/
             |   application.yml


src/
 +- main/
     +- webapp/
        +- WEB-INF/
           +- pages/
              | home.jsp

HomeController.java

package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String getHome() {
        System.out.println("Controller");
        return "home";
    }
}

application.yml

server:
  port: 8080
spring:
  mvc:
    view:
      prefix: /WEB-INF/pages/
      suffix: .jsp

build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
mainClassName = 'com.demo.SpringBootDemo'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

// In this section you declare where to find the dependencies of your project
repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('org.springframework.boot:spring-boot-starter-parent')
    compile('javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api')
}

jar {
  manifest {
    attributes(
      'Main-Class': 'com.demo.SpringBootDemo'
    )
  }
}

SpringBootDemo.java

package com.demo;

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

@SpringBootApplication(scanBasePackages="com.demo")
public class SpringBootDemo {

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

home.jsp

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    Hello World
</body>
</html>

When I try to execute the code, it shows in the output as

Controller

But it gives the Whitelabel Error Page when I visit http://localhost:8080/

Upvotes: 2

Views: 21032

Answers (2)

Techflash
Techflash

Reputation: 767

Run with maven spring boot goal: spring-boot:run

Steps to setup maven configuration in IntelliJ:

Debug/Run Configuration | Click on + button visible at top left | Select Maven | Set command line to spring-boot:run

Upvotes: 1

Ivonet
Ivonet

Reputation: 2741

UPDATE Ok I'll try to do better. It was not my intention to do you wrong...

I think you need to extend the SpringBootApplication class from SpringBootServletInitializer to get servlet functionality in your Spring Boot application

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {

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

In order to be able to resolve the view you have to define these properties:

spring.mvc.view.prefix: /WEB-INF/pages
spring.mvc.view.suffix: .jsp

or of course the yaml variant of them. The actual jsp pages need then be placed in src/main/webapp/WEB-INF/pages

These are the dependencies in my pom

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

I hope you can translate this to gradle yourself?

My sample controller looks like this:

@Controller
public class HelloController {
    @RequestMapping("/")
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}

Hope this will help you with getting your app working...

Upvotes: 3

Related Questions