springBoot之HelloWorld

开发工具:IDEA

SprintBoot版本:1.4.1

项目结构图:

核心注解类说明

@RestController
  就是@Controller+@ResponseBody组合,支持RESTful访问方式,返回结果都是json字符串

@SpringBootApplication
  @SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan等组合在一下

@SpringBootTest
  Spring Boot版本1.4才出现的,具有Spring Boot支持的引导程序(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)
关键是自动导入测试需要的类。。。

配置文件(pox.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jege.spring.boot</groupId>
    <artifactId>spring-boot-hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-hello-world</name>
    <url>http://maven.apache.org</url>

    <!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <!-- 自动包含以下信息: -->
        <!-- 1.使用Java6编译级别 -->
        <!-- 2.使UTF-8编码 -->
        <!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). -->
        <!-- 4.智能资源过滤 -->
        <!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). -->
        <artifactId>spring-boot-starter-parent</artifactId>
        <!-- spring boot 1.x最后稳定版本 -->
        <version>1.4.1.RELEASE</version>
        <!-- 表示父模块pom的相对路径,这里没有值 -->
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

        <!-- 测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- 只在test测试里面运行 -->
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring-boot-hello-world</finalName>
        <plugins>
            <!-- jdk编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

启动Application

控制器HelloWorldControler

测试类HelloWorldControllerTest

运行

运行Application的main方法,打开浏览器: 
http://localhost:8080/hello1 
输出Hello World 
http://localhost:8080/hello2 
输出[“A”,”B”,”C”]

运行HelloWorldControllerTest 
以Mock方式测试Controller

原文地址:https://www.cnblogs.com/Gxiaobai/p/9964696.html