[关闭]
@coldxiangyu 2017-06-06T07:16:09.000000Z 字数 2641 阅读 3145

IntelliJ IDEA 快速创建Spring boot

技术储备


在我的另一篇文章里,已经介绍过Spring boot,以及它的基本配置了。
那篇文章是在Myeclipse环境通过maven项目搭建起来的,虽然也很容易,但是还有更容易的。今天我要介绍的就是通过IDEA的Spring Initializr创建Spring boot工程。
接下来来看看到底有多容易。
在不用IntelliJ IDEA的情况下,我们通常需要访问http://start.spring.io/,生成maven包,然后解压导入到我们的IDE。
image_1bhu45e0gqkc125014gfi3m13n39.png-47.6kB
而IntelliJ IDEA与Spring结合的非常好,Spring Initializr已经内置,我们直接在IDEA进行创建即可。
image_1bhu253ei1ueo1ib0kj9177i1u8lp.png-48kB

接下来,就是http://start.spring.io/的选择界面了:

image_1bhu26qcj1v4ugd22sa1gqr1li516.png-33.1kB

再接下来,我们可以选择spring boot的版本,各种开发相关的依赖也集中在此处供我们选择,非常的人性化:

image_1bhu28ufdnjo1ei5i0g1k4cfj91j.png-47.1kB

我们只需简单创建,无需选择,点击下一步,完成即可。

image_1bhu2hpof142p1hir12pt1vpl14s220.png-63.7kB

我们可以看到,一个完整的spring boot项目已经展现在我们面前,包括:
src/main/java下的程序入口:DemoApplication
src/main/resources下的配置文件:application.properties
src/test/下的测试入口:DemoApplicationTests

其中自动生成的POM配置如下:

image_1bhu2u63q1jmt1m6d1dbc1vjqb632d.png-92.3kB

自动生成的POM已经非常全面了,不过少了一个spring-boot-starter-web,我们添加web依赖即可。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

我们改造入口类,实现一个hello world!

  1. package com.example.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. @Controller
  8. @SpringBootApplication
  9. public class DemoApplication {
  10. @RequestMapping("/")
  11. @ResponseBody
  12. public String home(){
  13. return "Hello world!";
  14. }
  15. public static void main(String[] args) {
  16. SpringApplication.run(DemoApplication.class, args);
  17. }
  18. }

启动该入口类,并访问http://localhost:8080/
image_1bhu3bhh618gq4iv2rbbo8q4k2q.png-10.8kB
成功!

此外,我们也可以通过生成的单元测试类进行模拟http请求:

  1. package com.example.demo;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.mock.web.MockServletContext;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import org.springframework.test.context.web.WebAppConfiguration;
  10. import org.springframework.test.web.servlet.MockMvc;
  11. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  12. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  13. import static org.hamcrest.Matchers.equalTo;
  14. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  15. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  16. @RunWith(SpringJUnit4ClassRunner.class)
  17. @SpringBootTest(classes = MockServletContext.class)
  18. @WebAppConfiguration
  19. public class DemoApplicationTests {
  20. private MockMvc mvc;
  21. @Before
  22. public void setUp() throws Exception {
  23. mvc = MockMvcBuilders.standaloneSetup(new DemoApplication()).build();
  24. }
  25. @Test
  26. public void getHello() throws Exception {
  27. mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
  28. .andExpect(status().isOk())
  29. .andExpect(content().string(equalTo("Hello World")));
  30. }
  31. }

image_1bhu3vnplmep4ru1r6r1sv3p9137.png-152.9kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注