RestController不能正常工作

时间:2019-03-27 08:43:47

标签: spring spring-boot spring-web

我使用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)

4 个答案:

答案 0 :(得分:0)

代码看起来不错,也许没有嵌套的依赖项,请尝试使用maven进行清理和构建,然后重新运行,就可以了。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

它适用于除2.1.3之外的任何版本的spring boot。 我正在使用jdk_1.8.0171。 我不确定春季启动2.1.3是否会影响最新版本。

答案 3 :(得分:0)

在早期版本的2.0.3之类的Spring Boot中,在控制台中,它用来显示所有映射。

FTP_TLS.prot_p()

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

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

但是,从您的代码段来看,我认为即使在日志中未将其显示为已映射,当您击中端点时仍然应该得到结果。

如果不是这种情况,并且您遇到错误,那么我只能想到一个问题,代码段中没有提到包的结构。 您的Controller类必须在程序包层次结构中的下面嵌套到具有main()方法的主SpringApplication类中,然后才对其进行扫描。确保是这种情况。

否则,您可以使用类似<version>2.1.3.RELEASE</version>