spring 引导中未调用 Rest Controller 方法
Rest Controller method not getting called in spring boot
我正在通过 spring 引导应用程序实施休息网络服务。
POM
<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>
application.properties
server.port=8082
server.contexPath=/services
logging.level.org.springframework.web = DEBUG
logging.level.com.myCompany= INFO
logging.file = ../logs/services.log
应用程序启动器class
@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 时,我在控制台中看到 Did not find handler method for [/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 {}
我没有看到消息 - 控制台中调用的帐户方法意味着控制器方法没有被调用 invoked.Can 请告诉我为什么 createAccount() 方法没有被调用?
控制器是 class Spring 工厂得到 identified/scanned?
是什么导致了这个错误?
- 您能帮助我们捕捉您的项目结构吗?
AccountRestController
应该在 Application
的同一个包(或子包)中。像这样:
- 你检查你的日志文件了吗?有日志吗?
您的 server.contexPath 应该是 server.contextPath。您在 contexPath 中遗漏了一个 T。
您的 class 关卡中没有请求映射。在 @RestController 之后,应该有 @RequestMapping("/services/*)
看起来你拼错了 属性,它应该是 server.contextPath,而不是 server.contexPath (w/o 't') 就像你的情况一样。
请将server.contexPath
改为server.contextPath
。
再试一次,它应该可以工作,因为没有额外的 config
或那里有代码。
http://www.baeldung.com/spring-boot-application-configuration
您的控制器应该在主 SpringBootApplication
文件的同一个包或任何子包中。
如果你想包含其他包或更高级别的控制器,那么你可以特别提到使用 componentScan annotation.
如下图所示,我的登录控制器不在 StudentServicesApplication.java
的同一个包或子包中。因此,我特别提到了要考虑使用 componentScan
的控制器的包名
我正在通过 spring 引导应用程序实施休息网络服务。
POM
<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>
application.properties
server.port=8082
server.contexPath=/services
logging.level.org.springframework.web = DEBUG
logging.level.com.myCompany= INFO
logging.file = ../logs/services.log
应用程序启动器class
@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 时,我在控制台中看到 Did not find handler method for [/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 {}
我没有看到消息 - 控制台中调用的帐户方法意味着控制器方法没有被调用 invoked.Can 请告诉我为什么 createAccount() 方法没有被调用?
控制器是 class Spring 工厂得到 identified/scanned?
是什么导致了这个错误?
- 您能帮助我们捕捉您的项目结构吗?
AccountRestController
应该在Application
的同一个包(或子包)中。像这样: - 你检查你的日志文件了吗?有日志吗?
您的 server.contexPath 应该是 server.contextPath。您在 contexPath 中遗漏了一个 T。
您的 class 关卡中没有请求映射。在 @RestController 之后,应该有 @RequestMapping("/services/*)
看起来你拼错了 属性,它应该是 server.contextPath,而不是 server.contexPath (w/o 't') 就像你的情况一样。
请将server.contexPath
改为server.contextPath
。
再试一次,它应该可以工作,因为没有额外的 config
或那里有代码。
http://www.baeldung.com/spring-boot-application-configuration
您的控制器应该在主 SpringBootApplication
文件的同一个包或任何子包中。
如果你想包含其他包或更高级别的控制器,那么你可以特别提到使用 componentScan annotation.
如下图所示,我的登录控制器不在 StudentServicesApplication.java
的同一个包或子包中。因此,我特别提到了要考虑使用 componentScan