我正在尝试使用一个简单的Hello world消息来响应Spring Boot(1.5.9)项目。但是,我发送的任何请求都会立即返回404异常。
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);
}
}
控制器/ 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")
}
请求页面(localhost:5000/hello
)时,出现404错误。此请求的日志中显示以下信息(here是一个要点,因为我不喜欢将其格式化为引用的方式):
2018-02-05 13:01:37.600 DEBUG 17448 --- [nio-5000-exec-2] o.a.coyote.http11.Http11InputBuffer:收到[GET / HTTP / 1.1
cache-control:no-cache User-Agent:PostmanRuntime / 7.1.1接受: / 主机: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] o.a.c.authenticator.AuthenticatorBase:安全检查请求GET / 2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] org.apache.catalina.realm.RealmBase:没有定义适用的约束2018-02-05 13:01:37.601 DEBUG 17448 --- [nio-5000-exec-2] o.a.c.authenticator.AuthenticatorBase:不受任何约束2018-02-05 13:01:37.604 DEBUG 17448 --- [nio-5000-exec-2] o.a.tomcat.util.net.SocketWrapperBase:套接字: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4bfc0a6:org.apache.tomcat.util.net.NioChannel@b945368:java.nio.channels.SocketChannel中[连接 local = / 127.0.0.1:5000 remote = / 127.0.0.1:53684]],从缓冲区读取:[0] 2018-02-05 13:01:37.604 DEBUG 17448 --- [nio-5000-exec-2] o.apache.coyote.http11.Http11Processor:套接字: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4bfc0a6:org.apache.tomcat.util.net.NioChannel@b945368:java.nio.channels.SocketChannel中[连接 local = / 127.0.0.1:5000 remote = / 127.0.0.1:53684]],状态: [OPEN_READ],声明:[OPEN]
我确定我错过了一些非常基本的东西,但我不能为我的生活找出什么。在此先感谢:)
答案 0 :(得分:0)
您提供的信息还不够。
当我使用这个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>
它适用于server.contextPath=/
以及注释时。
答案 1 :(得分:0)
此代码适用于我,除了以下代码之外没有单个字符可以运行spring boot simple API。我认为您的应用程序中存在一些配置类或pom问题。
<强>的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
成功运行