spring boot demo

17 篇文章 0 订阅
13 篇文章 0 订阅

说明

  • 项目基于idea+maven
  • 代码github位置:https://github.com/ydfind/java-blog/tree/master/firstboot
  • 最终项目目录:
    在这里插入图片描述

程序

1 idea新建maven项目,项目名称“first-boot”

在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190405145523655.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NuZGF5WVU=,size_16,color_FFFF
在这里插入图片描述

2 添加依赖,pom添加下面
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
  <!-- 继承spring boot的starter -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
  </parent>
  <dependencies>
    <!-- 添加web starter的依赖 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
3 编写控制器
package com.ydfind.blog.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ydfind
 * @create 2019-04-05 15:10
 */
@Controller
public class FirstBootController {
    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello World";
    }
}
4 编写启动类
package com.ydfind.blog.demo;

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

/**
 * @author ydfind
 * @create 2019-04-05 15:07
 */
@SpringBootApplication
public class FirstBootMainApp {

    public static void main(String[] args){
        SpringApplication.run(FirstBootMainApp.class, args);
    }
}
5 运行

启动FirstBootMainApp类的main函数:
在这里插入图片描述
在这里插入图片描述

服务器运行jar

若需要idea编译成jar,在centos运营,则pom.xml还需要

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

流程总结

  • 建立maven项目
  • pom添加parent的spring-boot-starter-parent(版本2.1.5.RELEASE),和spring-boot-starter-web;
  • 写启动类。@SpringBootApplication、SpringApplication.run(main.class, args);
  • 写controller类。@RestController,@GetMapping的hello函数
  • 启动程序,访问hello接口验证
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值