Under the new idea Spring Boot project and configure start

I. Procedure

① new idea to use a Spring Boot project
② modify pom.xml
③ modify application.properties
④ modify write a Hello Spring Boot the Controller
⑤ start the project visit

Second, detailed steps

1、File-->New-->Project

 

 2, and select Next Spring Initializr

 

 3, and then enter Next Artiface

 

 4, check the Web, we have chosen template recommended by the official Thymeleaf template engine, other frameworks, middleware, database can be selected according to need, and we do not need to manually add the configuration files, select Done Next

 

 Select the template engine

5, Finish can

 

 6, see the Spring Boot project directory structure

7, add the following in pom.xml

Note: If you choose the dependent mybatis when new projects, like the mongodb start time will complain, because they did not configure the connection information and data source mongodb at this time if you just want to test whether the project to build a successful first to comment

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-data-mongodb</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.mybatis.spring.boot</groupId>-->
            <!--<artifactId>mybatis-spring-boot-starter</artifactId>-->
            <!--<version>1.3.2</version>-->
        <!--</dependency>-->
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

 

 8、编写Hello Spring Boot的Controller

package com.example.bootopen.com;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloSpringBootController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot";
    }
}

 

 

9、修改配置文件 application.properties

注意:如果只是简单测试项目只需要添加端口即可,其他数据源、缓存、静态资源路径也可以在此配置。

 

 笔者推荐一种配置文件模式:另外新建2个配置文件,一个开发环境,一个线上环境,通过application.properties自由切换

 

 

 

 

10、启动项目 选择Run、Debug启动
关于@SpringBootApplication注解说明:@SpringBootApplication开启了Spring的组件扫描和springboot的自动配置功能,相当于将以下三个注解组合在了一起
(1)@Configuration:表名该类使用基于Java的配置,将此类作为配置类
(2)@ComponentScan:启用注解扫描
(3)@EnableAutoConfiguration:开启springboot的自动配置功能

 

 

 

 

 

 访问项目 http://localhost:8089/hello

 

 

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11839335.html