Spring Boot 学习之路一 使用IDEA 创建SpringBoot项目

使用IDEA 创建SpringBoot项目

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

项目结构为:

这里写图片描述


项目默认的 maven pom.xml
文件

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

    <groupId>com.jxust</groupId>
    <artifactId>spirngbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spirngbootdemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.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-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>

运行SpirngbootdemoApplication
的main方法,就能开始运行

下面来创建一个输出Hello SpringBoot 的视图。

创建一个HelloController,位于controller包下

这里写图片描述


HelloController.Java
 

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String say(){
        return "Hello SpringBoot!";
    }
}

在浏览器中输入http://localhost:8080/hello
就能输出Hello SpringBoot!
这句话。

这里写图片描述

自定义属性配置

用到的是application.properties这个文件

这里写图片描述

配置端口号和访问前缀
application.properties

server.port=8081
server.context-path=/springboot

这里写图片描述

除了使用.properties格式的文件,还可以使用.yml格式的配置文件(推荐),更加简便 application.yml ,这个下一节讲

猜你喜欢

转载自blog.csdn.net/run65536/article/details/81539355