SpringBoot(一)——IDEA创建项目

1.创建项目

在第一个NewProject界面的左边选择【Spring Initializer】后,在右边的【Project SDK】选择Java版本,下面选择【Default】,点击【Next】;

选择Java版本【Java Version】,自定义各个名称,点击【Next】;

右边选择【Web】,中间勾选【Spring Web】,然后选择SpringBoot版本,点击【Next】;

输入【Project name】,点击【Finish】;

顺便修改编码格式:Settings→Editor→FileEncodings,【Global Encoding】、【Project Encoding】、【Default encoding for properties files】设置为UTF-8编码,勾选【Transparent native-to-ascii conversion】;(防止后续开发过程会出现的乱码问题)

注解生效激活:Settings→Build,Execution,Deployment→Compiler→Annotation Processors,勾选【Enable annotation processing】;

确认Java编译版本:Settings→Build,Execution,Deployment→Compiler→Java Compiler;

2.main函数所在的启动类

  • 启动类只能识别当前包的下一级的内容;
  • @SpringBootApplication注解表示这是一个springboot主配置类。它是一个组合注解,即包含了其他多个注解封装出来的功能强大的新注解。例如包含的@SpringBootConfiguration表示这是一个springboot的配置类,@EnableAutoConfiguration表示自动配置的。这两个又包含了@Configuration,是Spring的组件,一层又一层;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Test01Application {

    public static void main(String[] args) {
        SpringApplication.run(Test01Application.class, args);
    }
}

3.控制器类

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

@RestController
public class HelloWorld {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

@RestController=@Controller+@ResponseBody 直接返回JSON数据格式
@Controller配合视图解析器InternalResourceViewResolver才能返回到指定页面
@ResponseBody与@Controller一起使用后返回JSON数据格式

4.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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lyj</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Test01</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency><!--SpringBoot启动依赖包-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency><!--SpringBoot测试依赖包-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency><!--启动web需要的依赖包-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency><!--单元测试-->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build><!--打包的,一般不用管-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5.测试类

在src/test/java文件下,测试类需要加上@SpringBootTest注解,方法需要加上@Test注解

如果SpringBoot版本是2.2以前的,需要加上@RunWith(SpringRunner.class)

import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;

@SpringBootTest
class Test01ApplicationTests {
    @Test
    public void testHello(){
        System.out.println("helloworld");
    }
}
原文地址:https://www.cnblogs.com/shoulinniao/p/13408924.html