簡體   English   中英

在 Spring Boot 中未調用 Rest Controller 方法

[英]Rest Controller method not getting called in spring boot

我正在通過 Spring Boot 應用程序實現 rest webservice。

聚甲醛

<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.myCompany</groupId>
    <artifactId>services</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>REST Services</name>
    <description>REST Services</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>   
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

應用程序屬性

server.port=8082
server.contexPath=/services
logging.level.org.springframework.web = DEBUG
logging.level.com.myCompany= INFO
logging.file = ../logs/services.log

應用程序啟動器類

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

控制器

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AccountRestController {

    private static final Logger logger = LoggerFactory.getLogger(AccountRestController.class);

    @RequestMapping(value="/account", method=RequestMethod.GET)
    public void createAccount(){
        logger.info("ACCOUNT METHOD CALLED");
    }
} 

當我在瀏覽器中觸發http://localhost:8082/services/account URL 時,我在控制台中看到沒有找到 [/services/account]消息的處理程序方法,如下所示

2018-02-06 08:30:42.868 DEBUG 7532 --- [http-nio-8082-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /services/account
2018-02-06 08:30:42.868 DEBUG 7532 --- [http-nio-8082-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/services/account]
2018-02-06 08:30:42.869 DEBUG 7532 --- [http-nio-8082-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/services/account] are [/**]
2018-02-06 08:30:42.869 DEBUG 7532 --- [http-nio-8082-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/services/account] are {}

我沒有在控制台中看到消息 - ACCOUNT METHOD CALLED 意味着控制器方法沒有被調用。你能告訴我為什么 createAccount() 方法沒有被調用嗎?
控制器類是否被 Spring 工廠識別/掃描?
是什么導致了這個錯誤?

請將server.contexPath更改為server.contextPath

再試一次,它應該工作,因為那里沒有多余的config或代碼。

http://www.baeldung.com/spring-boot-application-configuration

  1. 您能幫助我們掌握您的項目結構嗎? AccountRestController應該位於Application的同一程序包(或子程序包)中。 像這樣: 簡單愚蠢的項目結構
  2. 您檢查過日志文件了嗎? 有日志嗎?

您的server.contexPath應該是server.contextPath。 您錯過了contexPath中的T。

在您的課程級別沒有請求映射。 @RestController之后,應該有@RequestMapping(“ / services / *)

看起來您拼寫了該屬性,它應該是server.contex t路徑,而不是您所用的server.contexPath(w / oa't')。

你的控制器應該在你的主SpringBootApplication文件的同一個包或任何子包中。

如果您想在其他包或更高級別中包含控制器,那么您可以使用componentScan annotation.特別提及componentScan annotation.

如下圖所示,我的登錄控制器不存在於StudentServicesApplication.java的相同或子包中。 因此我特別提到了要考慮使用 componentScan 的控制器的包名稱

在此處輸入圖片說明

暫無
暫無

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

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