Dan
Dan

Reputation: 946

Spring type=Not Found, status=404 No message available

The only thing I have done so far is create a package called "controller" with a "HomeController.java" file inside it, with this code:

    package com.demo.spring.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @Controller
    public class HomeController {

        @RequestMapping(value = "/", method = RequestMethod.GET)
        @ResponseBody
        public String index(){

            return "Hello World.";
        }
    }

Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.demo.spring</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-spring</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

My file structure: enter image description here

Full error:

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jan 17 15:17:56 GMT 2018 There was an unexpected error (type=Not Found, status=404). No message available

Upvotes: 2

Views: 13019

Answers (3)

Venugopal vemula
Venugopal vemula

Reputation: 1

I received such error when 'SpringApplication' class and controller classes are in different packages.

Adding

@SpringBootApplication(scanBasePackages = "a.b.c")

fixed my issue

Upvotes: 0

Dan
Dan

Reputation: 946

I had a file missing called DemoSpringApplication.java. It should have been in com.demo.spring package.

This file should have been a default file. I'm not sure if I accidentally removed it or if it was never there. I created a new project inside IntelliJ and it showed in the new project automatically.

See this image compared to the above: enter image description here

Upvotes: 0

John Humphreys
John Humphreys

Reputation: 39314

If you're returning a raw string like this you should add the @ResponseBody annotation below your other annotation.

Alternatively, you can leave that off and do it like this. This says you're returning a valid HTTP response entity (200 code) with a string, and it will be converted into JSON automatically for you.

@RequestMapping(value = "whatever_path", method = RequestMethod.GET)
public ResponseEntity<String> getResult() {
    return new ResponseEntity<>("Hello World", HttpStatus.OK);
}

Upvotes: 3

Related Questions