首页 开发技术Java正文

【Spring Boot】1. 使用Idea创建Spring Boot项目

拾年之璐 Java 2020-08-22 22:08:12 104 0

Java

Spring Boot

1. 创建项目

1、 项目类型:Spring Initializr,选择Default:https://start.spring.io

2、输入项目信息,如下图所示:

3、选择项目信息

4、输入项目的保存目录等信息,点击【Finish】即可

2. 第一个Hello项目

1、首先确定pom.xml文件中的依赖等信息是否有遗漏。下面是初始项目的依赖:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ecxhit</groupId>
    <artifactId>hello-spring-boot-2</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>hello-spring-boot-2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、Application中写入Hello代码:

代码如下:

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/下的文件代码如下:

源码如下:

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<String> responseEntity = template.getForEntity(base.toString(),String.class);
        assertThat(responseEntity.getBody(),equalTo("Hello Spring Boot"));

    }

}

以上。


声明:本文由作者原创发布,转载请联系原作者。

评论

关于我们
这里是网站的说明
关注本站微信号,享受更多服务!
联系方式
电话:159******00
地址:武汉市武昌区
Email:cxh@cxh.work
邮编:430000
Copyright ©2019-2020.Powered by©Z-BlogPHP&拾年之璐  鲁ICP备18018164号