【问题标题】:RestController not working with no errorsRestController 无法正常工作
【发布时间】:2019-03-27 18:27:15
【问题描述】:

我使用 STS 创建了一个带有 spring-boot-starter-web 的 Spring Boot 应用程序。然后在子包中创建一个 RestController 并启动应用程序。它没有显示错误,但休息端点不起作用。点击 localhost:8080/ 会出现 404 错误。

我尝试使用@ComponentScanmaven cleanmaven install

TestApplication.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);
    }

}

TestController.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)

【问题讨论】:

  • 使用命令行运行命令java 时会看到什么?
  • 这段代码应该可以工作。确保没有任何类路径问题。如果您尝试通过 Maven (mvn spring-boot:run) 运行应用程序并尝试访问您提到的端点,会发生什么情况?
  • 它的运行方式与命令行中的 java -jar 命令相同。
  • 与 mvn spring-boot:run 相同的问题
  • 我也尝试用 jetty 替换 Tomcat。它给出了同样的问题。

标签: spring spring-boot spring-web


【解决方案1】:

代码看起来不错,可能一些嵌套依赖不存在,尝试用maven清理和构建,重新运行应该没问题。

【讨论】:

    【解决方案2】:

    从 pom.xml 文件中删除版本属性

    【讨论】:

      【解决方案3】:

      它适用于除 2.1.3 之外的任何版本的 spring boot。 我正在使用 jdk_1.8.0171 。 如果 spring boot 2.1.3,我不确定最新版本是否存在问题。

      【讨论】:

        【解决方案4】:

        在 Spring Boot 2.0.3 等早期版本中,在控制台中,它用于显示所有映射。

        <version>2.1.3.RELEASE</version>
        

        但是在您使用的 2.1.3 版本中,在启动 Spring Boot 应用程序时,它不会在控制台中显示映射。

        参考:eclipse spring boot console log does not print mapped controller info

        但是从您的代码 sn-ps 来看,我认为即使在日志中它没有显示为已映射,当您到达端点时,您仍然应该得到结果。

        如果不是这样,你得到一个错误,我只能想到一个问题,包结构不是代码 sn-ps 中提到的。 您的 Controller 类必须在包层次结构中嵌套到具有 main() 方法的主 SpringApplication 类的下方,然后只会扫描它。确保是这种情况。

        否则,您可以使用 @ComponentScan(basePackages = {"com.example.*"}) 之类的东西

        【讨论】:

          猜你喜欢
          • 2016-12-01
          • 1970-01-01
          • 2016-09-01
          • 2012-07-11
          • 2018-04-08
          • 2017-04-20
          • 2018-10-02
          • 2016-09-04
          • 2010-10-06
          相关资源
          最近更新 更多