spring boot2.0(一 ) 基礎環境搭建

1.基礎配置

開發環境:windowjava

jdk版本:1.8(spring boot2.0最低要求1.8)web

開發工具:eclipsespring

構建方式:maven3apache

2.POM配置文件

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aaron</groupId>
  <artifactId>framework</artifactId>
  <packaging>war</packaging>
  <version>1.0.0</version>
  <name>framework Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath /> 
    </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>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
    
        <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>

        <!-- 熱部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        
    </dependencies>
    
  <build>
    <finalName>framework</finalName>
    
    <plugins>
        <!-- Spring Boot包含一個Maven插件 ,能夠將項目打包爲可執行的jar。 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 沒有該配置,devtools 不生效 -->
                <fork>true</fork>
                <addResources>true</addResources>  
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

3.配置啓動類:

package com.aaron.framework;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


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

 

4.添加controller:

package com.aaron.framework.controller;

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

@RestController	//@RestController是返回json數據的controller,@Controller是返回頁面的controller
@RequestMapping("/")
public class HelloWorldController {
	
	@RequestMapping("/")
	public String HelloWorld() {
		return "helloWorld";
	}
}

 

5.運行測試:

6.頁面測試結果:json

相關文章
相關標籤/搜索