Spring Boot:无法访问本地主机上的REST控制器(404)

我正在尝试修改Spring Boot网站上的REST Controller示例。 不幸的是,当我尝试访问localhost:8080/item URL时,出现以下错误。

 { "timestamp": 1436442596410, "status": 404, "error": "Not Found", "message": "No message available", "path": "/item" } 

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>SpringBootTest</groupId> <artifactId>SpringBootTest</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <javaVersion>1.8</javaVersion> <mainClassPackage>com.nice.application</mainClassPackage> <mainClass>${mainClassPackage}.InventoryApp</mainClass> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${javaVersion}</source> <target>${javaVersion}</target> </configuration> </plugin> <!-- Makes the Spring Boot app executable for a jar file. The additional configuration is needed for the cmd: mvn spring-boot:repackage OR mvn spring-boot:run --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${mainClass}</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <!-- Create a jar with a manifest --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <mainClass>${mainClass}</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot. This replaces the usage of the Spring Boot parent POM file. --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- more comfortable usage of several features when developing in an IDE. Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it's started using a special classloader, then it is considered a 'production application'. Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </dependencyManagement> <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> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency> </dependencies> </project> 

起动器应用:

 package com.nice.application; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class InventoryApp { public static void main( String[] args ) { SpringApplication.run( InventoryApp.class, args ); } } 

REST的控制器:

 package com.nice.controller; @RestController // shorthand for @Controller and @ResponseBody rolled together public class ItemInventoryController { public ItemInventoryController() { } @RequestMapping( "/item" ) public String getStockItem() { return "It's working...!"; } } 

我正在用Maven构build这个项目。 以jar(spring-boot:run)的方式启动它,并在IDE(Eclipse)中启动它。

控制台日志:

 2015-07-09 14:21:52.132 INFO 1204 --- [ main] cbipseiaInventoryApp : Starting InventoryApp on 101010002016M with PID 1204 (C:\eclipse_workspace\SpringBootTest\target\classes started by MFE in C:\eclipse_workspace\SpringBootTest) 2015-07-09 14:21:52.165 INFO 1204 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7a3d45bd: startup date [Thu Jul 09 14:21:52 CEST 2015]; root of context hierarchy 2015-07-09 14:21:52.661 INFO 1204 --- [ main] osbfsDefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 2015-07-09 14:21:53.430 INFO 1204 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2015-07-09 14:21:53.624 INFO 1204 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2015-07-09 14:21:53.625 INFO 1204 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.23 2015-07-09 14:21:53.731 INFO 1204 --- [ost-startStop-1] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2015-07-09 14:21:53.731 INFO 1204 --- [ost-startStop-1] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 1569 ms 2015-07-09 14:21:54.281 INFO 1204 --- [ost-startStop-1] osbceServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2015-07-09 14:21:54.285 INFO 1204 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2015-07-09 14:21:54.285 INFO 1204 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2015-07-09 14:21:54.508 INFO 1204 --- [ main] swsmmaRequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7a3d45bd: startup date [Thu Jul 09 14:21:52 CEST 2015]; root of context hierarchy 2015-07-09 14:21:54.573 INFO 1204 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error]}" 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-07-09 14:21:54.573 INFO 1204 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest) 2015-07-09 14:21:54.594 INFO 1204 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-07-09 14:21:54.594 INFO 1204 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-07-09 14:21:54.633 INFO 1204 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-07-09 14:21:54.710 INFO 1204 --- [ main] osjeaAnnotationMBeanExporter : Registering beans for JMX exposure on startup 2015-07-09 14:21:54.793 INFO 1204 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2015-07-09 14:21:54.795 INFO 1204 --- [ main] cbipseiaInventoryApp : Started InventoryApp in 2.885 seconds (JVM running for 3.227) 2015-07-09 14:22:10.911 INFO 1204 --- [nio-8080-exec-1] oaccC[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2015-07-09 14:22:10.911 INFO 1204 --- [nio-8080-exec-1] osweb.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2015-07-09 14:22:10.926 INFO 1204 --- [nio-8080-exec-1] osweb.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms 

我到目前为止所尝试的是:

  • 使用应用程序名称访问URL(InventoryApp)
  • ItemInventoryController类级别放置另一个@RequestMapping("/")

就我所知,在使用Spring Boot时,我不需要应用程序上下文。 我对吗?

还有什么可以通过URL访问方法?

尝试将以下内容添加到InventoryApp类中

 @SpringBootApplication @ComponentScan(basePackageClasses = ItemInventoryController.class) public class InventoryApp { ... 

spring-boot会扫描com.nice.application包中的com.nice.application ,所以如果你的控制器在com.nice.controller你需要明确地扫描它。

添加到MattR的答案:

如此处所述, @SpringBootApplication自动插入所需的注释: @Configuration @EnableAutoConfiguration@ComponentScan @EnableAutoConfiguration@ComponentScan ; 但是, @ComponentScan ComponentScan只会查找与App相同的包中的组件,在本例中是com.nice.application ,而您的控制器驻留在com.nice.controller 。 这就是为什么你得到404,因为应用程序没有findapplication包中的控制器。

SpringBoot开发人员build议您将主应用程序类定位到其他类上方的根包中。 使用根包也允许使用@ComponentScan注释而不需要指定basePackage属性。 详细信息但请确保存在自定义根包。

我有这个问题,你需要做的是修复你的软件包。 如果你从http://start.spring.io/下载了这个项目,那么你在一些包里有你的主类。; 例如,如果主类的包是“com.example”,那么你的控制器必须包在“com.example.controller”中。 希望这可以帮助。

有两种方法可以解决这个问题

方法1)将启动应用程序放置在程序包结构的开始位置,并将所有控制器放在其中。例如:package com.spring.boot.app; – 你启动应用程序(即主要方法-SpringApplication.run(App.class,args);)

你rest控制器在相同的包结构例如:包com.spring.boot.app.rest;

方法2)在启动包中明确定义控制器。

方法1更清洁。

希望这会有所帮助。

您需要修改Starter-Application类,如下所示。

 @SpringBootApplication @EnableAutoConfiguration @ComponentScan(basePackages="com.nice.application") @EnableJpaRepositories("com.spring.app.repository") public class InventoryApp extends SpringBootServletInitializer {.......... 

如下所述更新Controller,Service和Repository包结构。

示例:REST控制器

package com.nice.controller; – >它必须修改为
package com.nice.application.controller;

你需要按照正确的包结构来处理Spring Boot MVCstream中的所有包。

所以,如果你修改你的项目捆绑软件包结构正确,那么你的春季启动应用程序将正常工作。

你的URL应该是,你缺less应用程序上下文:localhost:8080 / SpringBootTest / item