我对Spring Boot真的很陌生,因此我从一些教程开始创建一个简单的Rest示例(我的最终目标是将其与mongodb集成,但是我需要开始!)。我使用Spring Initializr在IntelliJ上创建了一个新项目,并选择了Web和Mongodb作为功能。然后我只添加了一个类,
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
当我运行项目时,控制台是
2018-09-03 20:08:33.139 INFO 1363 --- [ main] test.test.TestApplication : Starting TestApplication on MacBook-Pro-di-Stefano.local with PID 1363 (/Users/stefanomiceli/IdeaProjects/test/target/classes started by stefanomiceli in /Users/stefanomiceli/IdeaProjects/test) 2018-09-03 20:08:33.141 INFO 1363 --- [ main] test.test.TestApplication : No active profile set, falling back to default profiles: default 2018-09-03 20:08:33.173 INFO 1363 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@15713d56: startup date [Mon Sep 03 20:08:33 PDT 2018]; root of context hierarchy WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/Users/stefanomiceli/.m2/repository/org/springframework/spring- core/5.0.8.RELEASE/spring-core-5.0.8.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release 2018-09-03 20:08:33.950 INFO 1363 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-09-03 20:08:33.969 INFO 1363 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-09-03 20:08:33.969 INFO 1363 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32 2018-09-03 20:08:33.972 INFO 1363 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/stefanomiceli/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 2018-09-03 20:08:34.021 INFO 1363 --- [ost-startStop-1] o.a.c.c.C. [Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-09-03 20:08:34.021 INFO 1363 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 852 ms 2018-09-03 20:08:34.059 INFO 1363 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-09-03 20:08:34.061 INFO 1363 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-09-03 20:08:34.062 INFO 1363 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-09-03 20:08:34.062 INFO 1363 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-09-03 20:08:34.062 INFO 1363 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-09-03 20:08:34.143 INFO 1363 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-03 20:08:34.310 INFO 1363 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@15713d56: startup date [Mon Sep 03 20:08:33 PDT 2018]; root of context hierarchy 2018-09-03 20:08:34.344 INFO 1363 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-09-03 20:08:34.345 INFO 1363 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces= [text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-09-03 20:08:34.362 INFO 1363 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-03 20:08:34.362 INFO 1363 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-03 20:08:34.550 INFO 1363 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 2018-09-03 20:08:34.582 INFO 1363 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:55}] to localhost:27017 2018-09-03 20:08:34.585 INFO 1363 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 1]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1582641} 2018-09-03 20:08:34.739 INFO 1363 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-09-03 20:08:34.768 INFO 1363 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-09-03 20:08:34.772 INFO 1363 --- [ main] test.test.TestApplication : Started TestApplication in 1.834 seconds (JVM running for 7.413)
但是当我继续时,localhost:8080/我会404出错。我在 helloworld中 做错了什么?在运行应用程序之前,我应该做些什么吗?
localhost:8080/
404
您的包裹结构错误:
prova.HelloController test.test.TestApplication
HelloController必须处于同一级别或低于TestApplication。