Loading

Spring Boot菜鸟示例——Hello World

新建项目

  1. Next->Create
    在这里插入图片描述

  2. 等待下载依赖完成
    在这里插入图片描述

添加新类->一个HelloWord Controller类

在这里插入图片描述

鼠标放上自动提醒所依赖的库,可直接添加

在这里插入图片描述

编写Hello Word请求的Mapping

在这里插入图片描述

package cn.hiyj.idle.helloworld.controller;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

//@Controller与@ResponseBody的结合体,可返回字符串而不是返回一个“网页链接”
@RestController
//指定url的请求可以分发到指定的类、函数
@RequestMapping("hello")
public class HelloWordController {
    // 响应Get请求,只与GetMapping注解参数有关,与函数名无关,没有/开头的参数是指与类的相对路径,相对路径的无下级路径就是自己
    // http://localhost:8888/hello
    @GetMapping
    public String hello() {
        return "OK";
    }

    // 参数添加在请求路径中,例如http://localhost:8888/hello/WindSnowLi
    @GetMapping(value = "{name}")
    // 请求路径的name的值传递给name变量,@PathVariable路径中的变量
    public String name(@PathVariable String name) {
        return name;
    }

    // 参数添加在请求参数中,例如http://localhost:8888/hello/table?key=1&value=2中问号之后
    @GetMapping(value = "table")
    //请求参数的值传递给一个Map,@RequestParam注解的作用
    public String map(@RequestParam Map<String, String> table) {
        return table.toString();
    }
}

设置服务监听的端口号

在这里插入图片描述

运行启动服务

在这里插入图片描述

CMD curl命令测试

在这里插入图片描述

posted @ 2022-07-04 23:06  WindSnowLi  阅读(20)  评论(0编辑  收藏  举报