[英]Spring REST API controller is not called
i have following code 我有以下代码
Controller 调节器
@RestController
@RequestMapping("/")
public class RequestHandler {
@RequestMapping(value = "/demo", method = RequestMethod.POST)
@ResponseBody
public Object showDemo() {
return "Post method";
}
@RequestMapping(value = { "/getdemo" }, method = RequestMethod.GET)
@ResponseBody
public Object showgetDemo() {
System.out.println("hello");
return "get Method";
}
}
My pom xml 我的pom xml
<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>Website2015</groupId>
<artifactId>Website2015</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<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>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
and to launch Spring boot application : 并启动Spring启动应用程序:
@Configuration
@EnableWebMvc
@SpringBootApplication
public class AppLauncher {
public static void main(String[] args) {
SpringApplication.run(AppLauncher.class, args);
}
private static Class<AppLauncher> applicationClass = AppLauncher.class;
}
Server logs 服务器日志
2015-10-21 16:07:59.314 INFO 12892 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2015-10-21 16:07:59.470 INFO 12892 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2015-10-21 16:07:59.471 INFO 12892 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.15
2015-10-21 16:07:59.561 INFO 12892 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2015-10-21 16:07:59.561 INFO 12892 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1158 ms
2015-10-21 16:07:59.716 INFO 12892 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2015-10-21 16:07:59.719 INFO 12892 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-10-21 16:07:59.921 INFO 12892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2015-10-21 16:07:59.921 INFO 12892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2015-10-21 16:08:00.119 INFO 12892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@25af5db5: startup date [Wed Oct 21 16:07:58 CEST 2015]; root of context hierarchy
2015-10-21 16:08:00.275 INFO 12892 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2015-10-21 16:08:00.322 INFO 12892 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-10-21 16:08:00.323 INFO 12892 --- [ main] c.xx.yy.misc.AppLauncher : Started AppLauncher in 2.16 seconds (JVM running for 2.496)
I am using POSTMAN Rest client .All i am getting is 我正在使用POSTMAN Rest客户端。我得到的是
{ "timestamp": 1445436550585, "status": 404, "error": "Not Found", "message": "No message available", "path": "/get" } {“timestamp”:1445436550585,“status”:404,“error”:“Not Found”,“message”:“没有可用消息”,“path”:“/ get”}
i cant find mapping in server logs as well . 我也无法在服务器日志中找到映射。 My best guess after spending a lot of time is false project structure. 花费大量时间后我最好的猜测是错误的项目结构。
I created new Dynamic web project and converted it into Maven , ofcourse i tried it with simple java project and then converted it into maven . 我创建了新的Dynamic web项目并将其转换为Maven,当然我尝试使用简单的java项目然后将其转换为maven。
Please give me some pointers and if more info is required please let me know thanks. 请给我一些指示,如果需要更多信息,请告诉我们谢谢。
The default @SpringBootApplication
enables component scan only for the package of the application class and all subpackages. 默认的@SpringBootApplication
仅对应用程序类的包和所有子包启用组件扫描。 Since the package of AppLauncher is com.demo.misc and the package of RequestHandler is com.demo.controller, component scan won't detect the bean class. 由于AppLauncher包是com.demo.misc而RequestHandler包是com.demo.controller,因此组件扫描不会检测bean类。
Solution: 解:
If you are using Spring Boot 1.2.x: 如果您使用的是Spring Boot 1.2.x:
Either add the following annotation to your TestApp class: 将以下注释添加到TestApp类:
@ComponentScan({"com.demo.controller", "com.demo.misc"})
or move the RequestHandler to a subpackage of com.demo.misc, eg com.demo.misc.controller
或将RequestHandler移动到com.demo.misc的子包,例如com.demo.misc.controller
If you are already using Spring Boot 1.3.x: 如果您已经在使用Spring Boot 1.3.x:
Add the following parameter to your @SpringBootApplication
annotation: 将以下参数添加到@SpringBootApplication
批注中:
@SpringBootApplication(scanBasePackages = {"com.demo.controller", "com.demo.misc"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.