JAVA spring boot 微服务快速上手(01)

4 篇文章 0 订阅
3 篇文章 0 订阅

0. 准备条件

  • 安装JAVA JDK 1.8 并配置环境变量
  • 安装IntelliJ IDEA
  • 安装Maven

1. 创建项目

在IDEA中,点击File->New->Project,选择spring initializer,

 

点击Next,创建项目名称,下面的hawkzy,各位可以替换成自己喜欢的名字,工程名字就叫hello-world:

 

在左边和右边的窗口都选择Web:

 

一路next下去,会创建好工程,如下:

 

2. 修饰类:@RestController

在HelloWorldApplication类前增加修饰@RestController如下:

package com.hawkeye.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class HelloWorldApplication {

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

}

注意上面同时引入了org.springframework.web.bind.annotation.RestController类。

 

3. 创建并修饰接口

写接口hi,并用@GetMappin("/hi")修饰:

package com.hawkeye.helloworld;

import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class HelloWorldApplication {

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

    @GetMapping("/hi")
    public String hi() {
        return "hi, this is hawkzy's program";
    }
}

 

4. 配置服务端信息

在resources文件夹下增加application.yml文件,配置如下:

 

5. 运行服务

在HelloWorldApplication.java标签页上点击右键,选择Run 'HelloWorldApplication',如下:

 

如果一切正常,可以看到提示:

 

此时打开浏览器输入http://localhost:8888/hi,可以看到如下页面:

至此,第一个程序已经完成了,恭喜大家!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值