簡體   English   中英

RestController無法正常工作

[英]RestController not working with no errors

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

代碼看起來很好,也許一些嵌套的依賴項不存在,嘗試使用maven清理和構建,重新運行它應該沒問題。

從pom.xml文件中刪除version屬性

它適用於除2.1.3之外的任何版本的彈簧引導。 我使用的是jdk_1.8.0171。 如果彈簧啟動2.1.3,我不確定最新版本是否存在問題。

在早期版本的Spring Boot 2.0.3中,在控制台中,它用於顯示所有映射。

 <version>2.1.3.RELEASE</version> 

但是在使用2.1.3的版本中,啟動Spring Boot應用程序時它不會在控制台中顯示映射。

參考: eclipse spring boot console日志不會打印映射的控制器信息

但是從你的代碼片段中,我認為即使在日志中它沒有顯示為映射,但是當你點擊端點時仍然應該得到結果。

如果情況並非如此並且您收到錯誤,我只能想到一個問題,包結構不是代碼片段中提到的。 您的Controller類必須嵌套在具有main()方法的主SpringApplication類的包層次結構中,然后才會掃描它。 確保是這種情況。

否則,您可以使用類似@ComponentScan(basePackages = {"com.example.*"})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM