spring - RestController 无法正常工作
问题描述
spring-boot-starter-web
我使用 STS创建了一个 Spring Boot 应用程序。然后在子包中创建一个 RestController 并启动应用程序。它没有显示错误,但休息端点不起作用。点击 localhost:8080/ 会出现 404 错误。
我尝试使用@ComponentScan
,maven clean
和maven install
.
测试应用程序.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
测试控制器.java
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/")
public String test() {
System.out.println("test");
return "test";
}
}
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<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>
日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2019-03-27 14:05:56.588 INFO 12452 --- [ main] com.example.demo.TestApplication : Starting TestApplication on W10INMUM01D1184 with PID 12452 (C:\Users\mhamdule\Documents\workspace-sts-3.9.6.RELEASE\test\target\classes started by mhamdule in C:\Users\mhamdule\Documents\workspace-sts-3.9.6.RELEASE\test)
2019-03-27 14:05:56.591 INFO 12452 --- [ main] com.example.demo.TestApplication : No active profile set, falling back to default profiles: default
2019-03-27 14:05:57.207 INFO 12452 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-03-27 14:05:57.224 INFO 12452 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-27 14:05:57.224 INFO 12452 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-27 14:05:57.230 INFO 12452 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Java\jdk1.8.0_144\bin;D:\app\client\Administrator\product\12.1.0\client_1\bin;D:\Oracle\client\product\12.1.0\client_1;D:\Oracle\client\product\12.1.0\client_1\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;c:\sybase\dll;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\nodejs\;C:\Users\mhamdule\AppData\Local\Microsoft\WindowsApps;C:\Users\mhamdule\AppData\Local\Programs\Git\cmd;C:\Users\mhamdule\Downloads\spring-tool-suite-3.9.6.RELEASE-e4.9.0-win32-x86_64\sts-bundle\sts-3.9.6.RELEASE;;.]
2019-03-27 14:05:57.337 INFO 12452 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-27 14:05:57.337 INFO 12452 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 717 ms
2019-03-27 14:05:57.535 INFO 12452 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-27 14:05:57.538 INFO 12452 --- [ main] com.example.demo.TestApplication : Started TestApplication in 1.236 seconds (JVM running for 1.798)
解决方案
它适用于除 2.1.3 之外的任何版本的 spring boot。我正在使用 jdk_1.8.0171 。如果 spring boot 2.1.3,我不确定最新版本是否存在问题。
推荐阅读
- mysql - .NETCore 中的 System.ServiceProcess 服务控制器
- javascript - Javascript字符串替换问题
- python-3.x - 由于“禁止的内部错误代码”,构建管道失败
- azure-active-directory - 可以绕过单个功能的 Azure Function App Azure AD 身份验证并改为通过功能键公开它吗?
- elixir - 在应用程序中获取 elixir mix 发布版本
- ruby-on-rails - React:在组件中使用 Get Request 的结果(Ruby on Rails + CoffeeScript)
- bash - /bin/sh 不识别十六进制转义序列
- android - 将自定义列表视图从 MainActivity 更改为 ExampleFragment
- python - 将数据附加到 DataFrame
- php - 有没有一种简单的方法来创建一个在 session_destroy() 之后存在的变量?