使用SpringBoot搭建一个简单的web工程
最近在学习SpringBoot,想写在博客园上记录一下,如有错误之处还望指出.
首先创建一个maven工程,不用勾选骨架.
在pom.xml文件中添加如下内容,使工程变成Springboot应用.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
因为我们搭建的是web应用,必须添加spring-boot-starter-web依赖,添加如下依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
因为springboot内置了tomcat,所以不用添加tomcat插件。
然后就是在工程中创建一个有main方法的类,在类名上方添加@SpringBootApplication注解。
然后在main方法中添加一行:
SpringApplication.run(本类名.class,args);
其实这已经是一个web应用了,而且也可以直接启动了。需要再写一个Controller类,再类上添加@Controller或者@RestController注解(它俩都行,不同之处自行百度或者我后面的文章会说),方法上加个@RequestMapping("/hello"),这下全都大功告成,运行main方法,打开浏览器访问即可。
想将压力转化成动力就要让自己内心变得足够强大
浙公网安备 33010602011771号