本文共 3437 字,大约阅读时间需要 11 分钟。
一、创建Maven工程(无需选择模板)
二、导入依赖
1、添加父依赖:
spring-boot-starter-parent org.springframework.boot 2.0.5.RELEASE
2.0.5.RELEASE这个版本不是很新,但高版本容易报错。
2、添加web模块的starter(有些人管它叫起步依赖):
org.springframework.boot spring-boot-starter-web
spring-boot-starter-web给咱们整了个Web相关jar包的大礼包,你可能用不到的jar包也一次性给你下下来。。。不过的确省心!这叫按功能引入依赖集。
三、编写启动类MySpringBootApplication.java(名字随便取):
package cn.liuxingchang;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class); }}
@SpringBootApplication用来标注一个SpringBoot应用类,main方法不一定在应用类中,比如:
MySpringBootApplication是应用类(无main方法):
package cn.liuxingchang;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MySpringBootApplication {}
而main方法在另一个类(比如叫Main.java,名字随意)中:
package cn.liuxingchang;import org.springframework.boot.SpringApplication;public class Main { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class); }}
注意:SpringApplication.run(应用类.class),即run方法里是一个SpringBoot应用类的字节码对象。
四、编写Controller(MyController.java):
package cn.liuxingchang.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class MyController { @RequestMapping("/hello") @ResponseBody public String controller() { return "Hello SpringBoot!"; }}
这里@ResponseBody的作用:无需自己编写前端页面,把 return 返回值作为页面文字响应给用户。
其实,还可以用@RestController,这一个注解就实现了@Controller+@ResponseBody这两个注解的功能!
五、运行MySpringBootApplication.java中的main方法,并访问页面
1、启动时可在控制台看到一个LOGO(官方叫banner):
这个banner可以自定义哦:
2、在浏览器上访问,不好意思,你将看到:
给你响应了一个错误页面,还好不是服务器不存在,说明服务器还是启动了的。这点从日志中也可以看出来:
上面这行日志值得玩味,它说明了如下几点:
a、embedded.tomcat说明SpringBoot内嵌了一个Tomcat服务器;
b、Tomcat的http端口是8080;
c、context path是空(和"/"等效),即不用加项目名什么的,直接访问即可。
再回到错误页面的事。服务器存在,但访问不到咋办?
访问不到是因为咱们没写前端页面呀,比如index.jsp什么的。
不过咱们配了个Controller:
要不访问试试?
如果你看到的是上面这样的页面,那么恭喜你,SpringBoot入门成功!
最后附上源码:
项目结构:
pom.xml:
4.0.0 spring-boot-starter-parent org.springframework.boot 2.0.5.RELEASE org.example P078_HelloSpringBoot 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web
MyController.java:
package cn.liuxingchang.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class MyController { @RequestMapping("/hello") @ResponseBody public String controller() { return "Hello SpringBoot!"; }}
MySpringBootApplication.java:
package cn.liuxingchang;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class); }}
转载地址:https://liuxingchang.blog.csdn.net/article/details/109106393 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!