简体   繁体   中英

Spring boot always returns a 404 error

I'm trying to get a Spring Boot (1.5.9) project to respond with a simple Hello world message. However, any request I send it immediately returns a 404 exception.

MyBackendApplication

package net.mypackage.backend;

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

@SpringBootApplication
public class MyBackendApplication {

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

controllers/HelloController

package net.mypackage.backend.controllers;

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "Hello, world!";
    }

}

application.properties

server.port = 5000
server.contextPath=/
logging.level.org=DEBUG

build.gradle

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

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'net.mypackage'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-jersey')
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile("org.springframework.boot:spring-boot-starter-actuator")
}

When requesting a page ( localhost:5000/hello ), I get a 404 error. The following information shows in the log for this request ( here is a gist, because I don't like the way this is formatted as a quote):

2018-02-05 13:01:37.600 DEBUG 17448 --- [nio-5000-exec-2] oacoyote.http11.Http11InputBuffer : Received [GET / HTTP/1.1
cache-control: no-cache User-Agent: PostmanRuntime/7.1.1 Accept: / Host: 127.0.0.1:5000 accept-encoding: gzip, deflate Connection: keep-alive ] 2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] oacauthenticator.AuthenticatorBase : Security checking request GET / 2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] org.apache.catalina.realm.RealmBase : No applicable constraints defined 2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] oacauthenticator.AuthenticatorBase : Not subject to any constraint 2018-02-05 13:01:37.604 DEBUG 17448 --- [nio-5000-exec-2] oatomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4bfc0a6:org.apache.tomcat.util.net.NioChannel@b945368:java.nio.channels.SocketChannel[connected local=/127.0.0.1:5000 remote=/127.0.0.1:53684]], Read from buffer: [0] 2018-02-05 13:01:37.604 DEBUG 17448 --- [nio-5000-exec-2] o.apache.coyote.http11.Http11Processor : Socket: [org.apache.tomcat.util.net.NioEndpoint $NioSocketWrapper@4bfc0a6:org.apache.tomcat.util.net.NioChannel@b945368:java.nio.channels.SocketChannel[connected local=/127.0.0.1:5000 remote=/127.0.0.1:53684]], Status in: [OPEN_READ], State out: [OPEN]

I'm sure I missed something very basic, but I can't for the life of me figure out what. Thanks in advance :)

Info you provided is not enough.

When I tried your Application and Controller with this 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.example</groupId>
    <artifactId>mvc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MVC</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-web</artifactId>
        </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>

it works with both server.contextPath=/ and also when commented out.

This code works for me, there's no single character beyond the below code to run spring boot simple API. I think there's some config class or pom issue in your application.

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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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-web</artifactId>
        </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>

HelloController.java

package com.example.demo;

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "Hello, world!";
    }
}

DemoApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

application.properties

server.port = 5000
logging.level.org=DEBUG

Running successfully

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM