Eclipse搭建SpringBoot之HelloWorld


你的eclipse需要先安裝

Spring Tool Suite™

第一種方法(不建議,之所以貼上是因為探索的過程)

首先新建Maven工程

 勾選第一個按鈕,第三個是選擇working set ,你可以不選

下一步,配置工程信息,注意打包為jar

 

 

 打開pom.xml文件,添加spring-boot依賴

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 新建文件src/main/hello/HelloController.java

package hello;

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

@RestController
public class HelloController {

    @RequestMapping("/")
    public String hello(){
        return "Greetings from Spring Boot!";
    }
}

 

新建src/main/hello/Application.java

package hello;

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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        // http://localhost:8080/
        SpringApplication.run(Application.class, args);
    }
}

 

運行Application

出現一堆:

訪問localhost:8080/

 

 第二種方法(推薦)

File-》new-》other

 選擇starter項目

 下一步

 下一步

 勾選Web,Finish。可以看到項目結構與我們自己建的Maven有些不一樣。

新建一個Controller

package com.example.demo;

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

@RestController
public class HelloController {

    @RequestMapping("/")
    public String hello(){
        return "Welcome to start SpringBoot!";
    }
}

 

運行

 

 運行結果

 

 比起原來的Spring項目,構建過程簡單多了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM