快速构建 spring-boot 项目

1. 引入pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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.zjw</groupId>
  <artifactId>spring-boot-study</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.16.RELEASE</version>
  </parent>

  <dependencies>
    <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>
    </dependency>
  </dependencies>
</project>

2. 引入配置文件

2.1 引入application.yml

spring:
  application:
    name: spring-boot-study
server:
  port: 10000

2.2 引入logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
  <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
  <property name="LOG_HOME" value="./log"/>
  <!-- 日志前缀名称 -->
  <property name="LOG_NAME" value="spring-boot-study"/>
  <!-- 控制台输出 -->
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <!--格式化输出:
      %d表示日期,
      %thread表示线程名,
      %-5level:级别从左显示5个字符宽度,
      %logger{50}输出日志的类 50代表包名加类名的总长度限制,
      %msg:日志消息,%n是换行符-->
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
    </encoder>
  </appender>
  <!-- 按照每天生成日志文件 -->
  <appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!--日志文件输出的文件名-->
      <FileNamePattern>${LOG_HOME}/${LOG_NAME}-%d{yyyy-MM-dd}.log
      </FileNamePattern>
      <!--日志文件保留天数-->
      <MaxHistory>30</MaxHistory>
    </rollingPolicy>
    <encoder
      class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <!--格式化输出:
      %d表示日期,
      %thread表示线程名,
      %-5level:级别从左显示5个字符宽度,
      %logger{50}输出日志的类 50代表包名加类名的总长度限制,
      %msg:日志消息,%n是换行符-->
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
    </encoder>
    <!--日志文件最大的大小-->
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>10MB</MaxFileSize>
    </triggeringPolicy>
  </appender>
  <!--库日志输出级别 -->
  <root level="INFO">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="ROLLING_FILE"/>
  </root>
</configuration>

3 编写启动类和Controller类

3.1 启动类

package com.zjw;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

  private static final Logger logger = LoggerFactory.getLogger(MyApplication.class);
  
  public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
    logger.info("Spring Application Running...");
  }
}

3.2 Controller类

package com.zjw.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
  private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

  @RequestMapping("/hello")
  public String hello() {
    logger.info("HelloController hello function invoked.");
    return "Hello, I'm Spring boot Application.";
  }
}

4 编写测试类

package com.zjw.test;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class HelloControllerTest {
  private static final Logger logger = LoggerFactory.getLogger(HelloControllerTest.class);

  @LocalServerPort
  private int port;
  private URL base;
  @Autowired
  private TestRestTemplate template;

  @Before
  public void before() throws MalformedURLException {
    this.base = new URL("http://localhost:" + port + "/hello");
  }

  @Test
  public void testHello() {
    ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
    logger.info(response.getBody());
    assertThat(response.getBody(), equalTo("Hello, I'm Spring boot Application."));
  }
}

附项目结果截图

在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页