1. 程式人生 > >01-SpringBoot之初體驗——Hello SpringBoot

01-SpringBoot之初體驗——Hello SpringBoot

第一個SpringBoot程式:Hello SpringBoot!

  1. 使用IDEA新建一個 Spring Initializr工程;
  2. 填寫Group和Artifact;
  3. 選擇Web依賴,Spring Boot預設選擇最新版本(目前2.1.0);
  4. 選擇專案位置;
  5. Finish;

主要專案檔案說明

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.senmentfault</groupId> <artifactId>spring-boot-lesson-1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>
jar</packaging> <name>spring-boot-lesson-1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.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>

2. SpringBootApplication啟動類

package com.senmentfault.springbootlesson1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootLesson1Application {

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

3. application.yml配置檔案

server:
  port: 80
  servlet:
    context-path: /test
person:
  name: Jun
  age: 31

4. HelloController.java

package com.senmentfault.springbootlesson1.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jun
 * 14:45 2018/11/5
 */
@RestController
public class HelloController {

    @Value("${person.name}")
    private String name;

    @Value("${person.age}")
    private Integer age;

    @RequestMapping(value="hello",method = RequestMethod.GET )
    public String sayHello() {
        return "Hello Spring Boot";
    }

    @RequestMapping(value="info",method = RequestMethod.GET )
    public String sayInfo(){
        String str ="姓名:"+name+"\n;年齡:"+age;
        return str;
    }

    @RequestMapping(value="mapinfo",method = RequestMethod.GET )
    public Map<String,Object> sayMapInfo(){
        String str ="姓名:"+name+"\n;年齡:"+age;
        Map<String,Object> map =new HashMap<>();
        map.put("name",name);
        map.put("age",age);
        return map;
    }
    }

5. 執行結果

6. 原始碼下載