使用idea搭建Spring boot开发初始环境
Posted 伟大是熬出来的
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用idea搭建Spring boot开发初始环境相关的知识,希望对你有一定的参考价值。
准备工作
将以下代码加入idea的live template,命名为springbootStartup
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Create New Project
![技术分享](http://upload-images.jianshu.io/upload_images/849366-55370c3b51fa3f13.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-3b05d1d34be1135e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-1dfe37014643294e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-8b881e1cabb718a1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-46c81ad268c6c1a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-cce300884fd30ea9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加maven支持
添加pom.xml
在project的根目录上的右键菜单中选择"Add Framework Support",添加maven支持。
![技术分享](http://upload-images.jianshu.io/upload_images/849366-166324ac6cacb01f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-7fc1f66837440c4d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-e7aca60b0b865a56.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
设置maven坐标
![技术分享](http://upload-images.jianshu.io/upload_images/849366-deb3640152f66233.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
补全maven其他设置
在pom.xml中,坐标设置下面输入sp,IDE自动弹出前面设置的live template:“springbootStartup”,按下"tab"键盘,剩余的maven代码将会自动补全。
![技术分享](http://upload-images.jianshu.io/upload_images/849366-176d6b36d8ca3575.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-502db3d9a9edc9d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
新建包结构
![技术分享](http://upload-images.jianshu.io/upload_images/849366-ff3176edb532775d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
新建application类
![技术分享](http://upload-images.jianshu.io/upload_images/849366-c83afc896cc0290b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-0adecb8dd135575e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-83ac90467f1ab436.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-35d37bdd96652e27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享](http://upload-images.jianshu.io/upload_images/849366-93530e5e99bf404c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
导入:
import org.springframework.boot.SpringApplication;
在main函数中添加如下代码:
SpringApplication.run(SpringBootDemoApplication.class, args);
添加controller类
![技术分享](http://upload-images.jianshu.io/upload_images/849366-1ab4dd580ad75976.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加@RestController
![技术分享](http://upload-images.jianshu.io/upload_images/849366-b80aa2d4791097bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
添加request mapping代码
@RequestMapping("/hello") String hello() { return "this is a test"; }
![技术分享](http://upload-images.jianshu.io/upload_images/849366-935678b16d392da2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
测试
![技术分享](http://upload-images.jianshu.io/upload_images/849366-fe24d777662220d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
第一次启动报错,显示8080端口已经被占用
![技术分享](http://upload-images.jianshu.io/upload_images/849366-a136490bc6ccccb0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
因此需要修改端口号,操作如下:
在resources目录下新建application.properties, 输入如下内容:
server.port=8081
然后重新启动程序,在浏览器中访问地址:
http://localhost:8081/hello
![技术分享](http://upload-images.jianshu.io/upload_images/849366-f4a1ea2f90a39b33.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以上是关于使用idea搭建Spring boot开发初始环境的主要内容,如果未能解决你的问题,请参考以下文章