我是spring-boot的新手,并创建了一个具有一个控制器方法的虚拟应用程序。
应用程序的包装类型在pom文件中以jar形式提及。 部署后,日志中没有错误。
但是在成功构建和部署后,当我尝试从浏览器访问本地主机时,仍然出现404错误。
下面是我已经尝试解决的问题:
1)在同一包中包含我的主类和控制器类。 2)我的主类放在根包中,控制器放在子包中。 3)在类和方法上定义@RestController。 4)在主类中扩展SpringBootServletInitializer
package com.test.controller;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
**Controller class**
package com.test.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class TestController {
public final Logger logger = LoggerFactory.getLogger(TestController.class);
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
logger.info("This is a test message to check if logback is working");
return "Hello World";
}
}
因此,根据结果,我应该在网页上收到Hello World消息,但是却收到404错误。
下面是登录服务启动:
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\Repositories\Personal\Mindship\resume-parser "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\plugins\maven\lib\maven3\bin\m2.conf" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1.3 spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building resume-parser 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) > test-compile @ resume-parser >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ resume-parser ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ resume-parser ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ resume-parser ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Repositories\Personal\Mindship\resume-parser\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ resume-parser ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) < test-compile @ resume-parser <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) @ resume-parser ---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2019-07-19 14:21:43.166 INFO 7384 --- [ main] c.r.controller.ResumeParserApplication : Starting ResumeParserApplication on WKSBAN09VDF8243 with PID 7384 (C:\Repositories\Personal\Mindship\resume-parser\target\classes started by ankit.srivastava in C:\Repositories\Personal\Mindship\resume-parser)
2019-07-19 14:21:43.187 INFO 7384 --- [ main] c.r.controller.ResumeParserApplication : No active profile set, falling back to default profiles: default
2019-07-19 14:21:47.574 INFO 7384 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-07-19 14:21:47.685 INFO 7384 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-07-19 14:21:47.689 INFO 7384 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-19 14:21:47.956 INFO 7384 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-07-19 14:21:47.956 INFO 7384 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4599 ms
2019-07-19 14:21:48.900 INFO 7384 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-19 14:21:49.338 INFO 7384 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-19 14:21:49.338 INFO 7384 --- [ main] c.r.controller.ResumeParserApplication : Started ResumeParserApplication in 7.906 seconds (JVM running for 17.28)
2019-07-19 14:22:55.499 INFO 7384 --- [nio-8080-exec-8] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-07-19 14:22:55.500 INFO 7384 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-07-19 14:22:55.528 INFO 7384 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Completed initialization in 20 ms