【SpringBoot】从零开始之创建项目(一)

前言

这个系列仅仅只是记录笔者学习SpringBoot过程中的心得,如有错误,欢迎指正。

准备

下载eclipse

  • 1.创建项目

选择Maven Project,记得勾选Use default Workspace location

image.png

填写详细信息

image.png

此时,项目结构为:
image.png

pom.xml添加SpringBoot相关的maven


    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

    

        
        1.7
        UTF-8
        UTF-8

        
        5.0.5.RELEASE

    
    
        
        
            org.springframework
            spring-context
        
        
            org.springframework
            spring-tx
        

        
            org.springframework.boot
            spring-boot-starter-web
        

    
  • 2.添加配置文件

src/main/resources下创建application.properties文件,这里是SpringBoot的配置文件,后续会有更多配置

server.port=8080
server.tomcat.uri-encoding=UTF-8
  • 3.创建程序入口

创建MyApplication,这里是程序的入口

@SpringBootApplication
public class MyApplication {

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

}
  • 4.创建Controller
    创建IndexController,这里的语法和SpringMvc一致
    @RestController 表示返回json格式
    @RequestMapping 表示接口的地址
    @GetMapping 表示支持get请求的接口地址
@RestController
@RequestMapping("/index")
public class IndexController {
    
    @GetMapping("/helloworld")
    public String index() {
        return "helloworld1";
    }
}
  • 5.运行项目

在MyApplication这个类中右键选择Run As# Spring Boot App,运行项目

image.png

在控制台看到如下输出,即表示运行成功

在浏览器输入http://localhost:8080/index/helloworld

image.png

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

你可能感兴趣的:(【SpringBoot】从零开始之创建项目(一))