初學spring boot,按照網上教程,用eclipse +maven+spring boot 創建了一個項目,controller非常簡單,代碼如下:
package com.example.demo.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class demoController
{
@ResponseBody
@RequestMapping("demo")
public String demo()
{
return "hello world;hello spring boot";
}
}
運行後,console出現以下訊息:
2019-07-05 05:08:58.880 INFO 10420 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on LAPTOP-FAS0SHLM with PID 10420 (C:\Users\yueho\eclipse-workspace\demo\target\classes started by yueho in C:\Users\yueho\eclipse-workspace\demo)
2019-07-05 05:08:58.886 INFO 10420 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-07-05 05:09:00.430 INFO 10420 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-07-05 05:09:00.470 INFO 10420 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-07-05 05:09:00.470 INFO 10420 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2019-07-05 05:09:00.480 INFO 10420 --- [ main] 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: [C:\Program Files\Java\jdk1.8.0_92\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_92/bin/server;C:/Program Files/Java/jre1.8.0_92/bin;C:/Program Files/Java/jre1.8.0_92/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Java\jdk1.8.0_92\bin;C:\Program Files\Java\jdk1.8.0_92\jre\bin;C:\Users\yueho\AppData\Local\Microsoft\WindowsApps;;C:\Users\yueho\OneDrive\Desktop;;.]
2019-07-05 05:09:00.690 INFO 10420 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-07-05 05:09:00.690 INFO 10420 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1732 ms
2019-07-05 05:09:01.029 INFO 10420 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-05 05:09:01.288 INFO 10420 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-05 05:09:01.294 INFO 10420 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.946 seconds (JVM running for 4.484)
但我在eclipse自帶瀏覽器輸入:http://localhost:8080/,卻出現以下訊息:
The webpage cannot be found
請問問題出在哪裏?
我用的tomcat是maven內崁的,請問如何配置?
@RequestMapping("demo")
你写了接口请求路径,那你应该在http://localhost:8080/demo去访问这个接口得到hello world
,如果你的项目还设置有applicationcontext,那你应该再加一层应用名称,如http://localhost:8080/baidu/demo
你这个是访问tomcat的,如果tomcat没配置好控制台就会出现这个问题,你的路径后面得加上工程名
spring boot的mvc其实还是spring mvc,你的demo里面的@RequestMapping("demo")是restful路径是demo,那么你范围的路径也应该是demo
https://start.spring.io/ 可以直接在网上配置创建并打包下载,在运行不行就是你环境有问题了
执行到你写的方法需要通过url路径去访问controller中的方法,url路径是由你的IP地址+项目tomcat端口+controller类上的@RequestMapping注解值+方法上的@RequestMapping("demo")注解值组成
你的请求路径都不对,可以直接在github下载这个demo运行一下,https://github.com/francisoyc/my-first-docker