Eclipse搭建SpringBoot之HelloWorld

你的eclipse须要先安装java

Spring Tool Suite™

第一种方法(不建议,之因此贴上是由于探索的过程)

首先新建Maven工程web

 勾选第一个按钮,第三个是选择working set ,你能够不选spring

下一步,配置工程信息,注意打包为jarapp

 

 

 打开pom.xml文件,添加spring-boot依赖eclipse

    <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.javamaven

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.javaspring-boot

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);
    }
}

 

运行Applicationui

出现一堆:spa

访问localhost:8080/code

 

 第二种方法(推荐)

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项目,构建过程简单多了

相关文章
相关标签/搜索