1. 程式人生 > >springboot入門一,使用myEclipse新建一個springboot項目

springboot入門一,使用myEclipse新建一個springboot項目

默認 引入 hot tid quest org 常用 告訴 tomcat8

1.環境信息
MyEclipse2015,jdk1.8,tomcat8

2.新建maven項目

2.1 新建一個web項目

技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片

2.2 生成的項目結構如下

技術分享圖片

3.配置pom.xml文件

 3.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.qfx</groupId>
    <artifactId>springbootDemo01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>springbootDemo01</name>
    <description>這裏填寫描述信息</description>

    <!-- 設置父類,整合第三方常用框架依賴信息(各種依賴信息) -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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>
        <!-- 引入springboot核心包,整合SpringMVC Web組件 -->
        <!-- 實現原理:Maven依賴繼承關系,相當於把第三方常用Maven依賴信息,在parent項目中已經封裝好了 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入springboot測試包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- springboot外部tomcat支持,用於編譯jsp -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- 打war包時加入此項, 告訴spring-boot tomcat相關jar包用外部的,不要打進去 -->
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <!-- 指定war包名稱,以此處為準,否則會帶上版本號 -->
        <finalName>springbootDemo01</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3.2 修改完畢,如果你的項目名上出現一個紅色的小叉號

技術分享圖片

請按照下圖進行操作即可

技術分享圖片

4.創建springboot啟動類DemoApp.java

代碼:

package com.qfx.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * <h5>描述:添加外部tomcat支持,需要繼承SpringBootServletInitializer,並重寫configure方法</h5>
 * 
 */
@SpringBootApplication
public class DemoApp extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApp.class);
    }

    public static void main(String[] args) {
        // 整個程序入口,啟動springboot項目,創建內置tomcat服務器,使用tomct加載springmvc註解啟動類
        SpringApplication.run(DemoApp.class, args);
    }
}

5.創建一個Conttoller

代碼:

package com.qfx.demo.controller;

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

@RestController
public class UserController {

    @RequestMapping("getUser")
    public String getUser(){
        return "張三";
    }
}

6.編譯項目

技術分享圖片

7.啟動springboot,執行DemoApp類的main方法

技術分享圖片

8.測試訪問:http://127.0.0.1:8080/getUser

使用springboot內置tomcat啟動,默認省略項目名,端口默認8080,訪問結果如下圖,表示啟動成功

技術分享圖片

9.最後附項目完整結構

技術分享圖片

springboot入門一,使用myEclipse新建一個springboot項目