SpringBoot系列--(1)--1.x 初体验 Hello World

版权声明:编码不易,禁止转载 https://blog.csdn.net/u_ascend/article/details/82385040

写在前面

        听说spring全家桶中有一个项目叫做SpringBoot,开发起来丝丝润滑。这个系列是我在学习过程中记录的笔记。谨防遗忘。期间不乏有参考各位大侠。先在此道谢。

        SpringBoot官方手册:https://docs.spring.io/spring-boot/docs/1.5.15.RELEASE/reference/htmlsingle/

SpringBoot简介

        Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

特点

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标,健康检查和外部配置
  6. 绝对没有代码生成并且对XML也没有配置要求

HelloWorld

1、访问官方网址:https://start.spring.io/

2、选择项如下图(有图有真相)

3、点击绿色的按钮把生成的压缩文件存储到本地(建议路径不要有中文和空格),解压。

4、在IDEA中:File-->New-->Project from Existing Sources...选则解压后的文件夹(demo)

5、一路next,一般没什么问题(不过不排除你的配置和笔者有出入,下面是笔者的选项)

6、JDK版本,笔者选择了1.8,导入完成后选择在新窗口中打开。如果idea设置了自动import,则idea可能会一直卡在resolve...某个jar上。解决:执行下面的命令(鼠标双击Maven Projects中的clean不好使,原因未知)

7、导入完成后的目录结构如下:

8、启动DemoApplication.main方法就可以了,效果如下:

搭建web项目

1、上面的HelloWorld只是最简单的配置,没有引入任何的模块,下面我们引入web模块。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

其中:

spring-boot-starter:核心模块,包括自动配置支持、日志和YAML

spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito

spring-boot-starter-web:web模块支持

2、创建package: cn.edu.zua.demo.controller,创建HelloWorldController:

package cn.edu.zua.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * HelloWorldController
 *
 * @author adeng
 * @date 2018/9/4 16:46.
 */
@RestController
public class HelloWorldController {
    @RequestMapping(path = "/hello")
    public String index() {
        return "Hello World";
    }
}

3、启动DemoApplication.main方法,在浏览器地址栏中访问:http://localhost:8080/hello

        我们就能看到“Hello World”被输出到浏览器中。小激动。

接口单元测试

1、经过上面的操作,都进行了简单的Hello World体验,下面是单元测试

2、在src/test/java/ 下创建package: cn.edu.zua.demo.controller,创建HelloWorldControllerTest:

package cn.edu.zua.demo.controller;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

/**
 * HelloWorldControllerTest
 *
 * @author adeng
 * @date 2018/9/4 17:17.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class HelloWorldControllerTest {
    private static MockMvc mvc;

    @BeforeClass
    public static void setUp() {
        mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
    }

    @Test
    public void testIndex() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }
}

3、重启项目,然后运行单元测试。

我不提供源码演示地址,为的是有一个切身的操作体验。直接下载下载运行有何意义?

各位看官,如果觉得写的不错就点个赞吧。求你了。

看这里,看这里

文章总目录:博客导航

 码字不易,尊重原创,转载请注明:https://blog.csdn.net/u_ascend/article/details/82385040

猜你喜欢

转载自blog.csdn.net/u_ascend/article/details/82385040