正文 【Spring Boot】1. 使用Idea创建Spring Boot项目 拾年之璐 V管理员 /2020年 /295 阅读 0901 [TOC] ### 1. 创建项目 1、 项目类型:Spring Initializr,选择Default:https://start.spring.io  2、输入项目信息,如下图所示:  3、选择项目信息  4、输入项目的保存目录等信息,点击【Finish】即可  ### 2. 第一个Hello项目 1、首先确定pom.xml文件中的依赖等信息是否有遗漏。下面是初始项目的依赖: ```html 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.13.RELEASE com.ecxhit hello-spring-boot-2 1.0.0-SNAPSHOT hello-spring-boot-2 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin ``` 2、Application中写入Hello代码:  代码如下: ```java package com.ecxhit.hellospringboot2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class HelloSpringBoot2Application { @RequestMapping(value = "",method = RequestMethod.GET) public String sayHi(){ return "Hello Spring Boot"; } public static void main(String[] args) { SpringApplication.run(HelloSpringBoot2Application.class, args); } } ``` 3、右击,运行项目(Ctrl+Shitf+F10)   4、浏览器输入地址,即可访问:  ### 3、自定义Banner(佛祖保佑)  示例: ``` _ooOoo_ o8888888o 88" . "88 (| ^_^ |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . ___ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ========`-.____`-.___\_____/___.-`____.-'======== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 佛祖保佑 永不宕机 永无BUG ``` 如果需要自定义字符,可以百度 ### 4、单元测试 test/java/下的文件代码如下:  源码如下: ```java package com.ecxhit.hellospringboot2; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import java.net.URL; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(classes = HelloSpringBoot2Application.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloSpringBoot2ApplicationTests { @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate template; @Before public void setUp() throws Exception{ this.base = new URL("http://localhost:"+port+"/"); } @Test public void contextLoads() { ResponseEntity responseEntity = template.getForEntity(base.toString(),String.class); assertThat(responseEntity.getBody(),equalTo("Hello Spring Boot")); } } ``` 至此。 本文采用创作共用版权 CC BY-NC-SA 3.0 CN 许可协议,转载或复制请注明出处! -- 展开阅读全文 --