本文共 1458 字,大约阅读时间需要 4 分钟。
第一次接触SpringBoot,根据官网在pom.xml中导入相应依赖后,首先,新建controller包,在其中写HelloController类:
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;@Controllerpublic class HelloController { @ResponseBody @RequestMapping("/hello")public String sayHello() { return "hello,This is my first Spring Boot.";}}
再写MyBootRun类,用它的主方法来运行Boot:
package com.bit.springboot.po;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MyBootRun { public static void main(String[] args) { SpringApplication.run(MyBootRun.class,args); }}
这时执行主方法,控制台的输出为:
它说应用程序启动失败。
配置为监听端口8080的Tomcat连接器启动失败。端口可能已经在使用中,或者连接器可能配置错误。
要解决这个问题,需要验证连接器的配置,识别并停止正在监听端口8080的任何进程,或将此应用程序配置为监听另一个端口。
在cmd命令行使用“netstat -ano”,查看端口状态:
可以看到我的8080端口是被3508应用进程占用着。
还可以用命令netstat -aon|findstr “8080"快速查找。
找到进程之后,就需要去结束它,即可解决8080端口被占用的问题。
这时再去执行主方法。…一件神奇的事情出现了,我的8080端口又被占用了,这回PID是6655,查看任务管理器,又是java.exe…于是再去任务进程中关闭java.exe,再去执行主方法,这时,在浏览器输入:http://localhost:hello,显示结果:
这是第二种错误,根据别人的博客,我意识到这个是项目目录的问题,SpringBoot读取不到映射。我的项目目录是这的:
哦,我犯了个错误,把MyBootRun类放在了po包下,po持久层应该是用来放映射成的类的。现在改变目录结构,把MyBootRun类放在com.bit.springboot包下:
(又发生了一次java.exe占用8080端口的问题,又去任务管理器中将它结束。)
在浏览器的url栏中输入"http://localhost:8080/hello”:
Okay,现在问题解决。
转载地址:http://zdjh.baihongyu.com/