我有以下代码
控制器
@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";
}
}
我的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>
并启动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;
}
服务器日志
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)
我正在使用POSTMAN Rest客户端。我得到的是
{ “timestamp”:1445436550585, “身份”:404, “错误”:“未找到”, “message”:“没有可用消息”, “path”:“/ get”}
我也无法在服务器日志中找到映射。 花费大量时间后我最好的猜测是错误的项目结构。
我创建了新的Dynamic web项目并将其转换为Maven,当然我尝试使用简单的java项目然后将其转换为maven。
请给我一些指示,如果需要更多信息,请告诉我们谢谢。
答案 0 :(得分:10)
默认@SpringBootApplication
仅对应用程序类的包和所有子包启用组件扫描。由于AppLauncher包是com.demo.misc而RequestHandler包是com.demo.controller,因此组件扫描不会检测bean类。
<强>解决方案:
如果您使用的是Spring Boot 1.2.x:
将以下注释添加到TestApp类:
@ComponentScan({"com.demo.controller", "com.demo.misc"})
或将RequestHandler移动到com.demo.misc的子包中,例如com.demo.misc.controller
如果您已经在使用Spring Boot 1.3.x:
将以下参数添加到@SpringBootApplication
注释:
@SpringBootApplication(scanBasePackages = {"com.demo.controller", "com.demo.misc"})