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

posted @   露娜妹  阅读(50928)  评论(4编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示