• Guide1:Building a RESTful Web Service


    This guide walks you through the process of creating a "Hello World"RESTful web service with Spring.

    You will build a service that will accept HTTP GET requests at
    http://localhost:8080/greeting or http://localhost:8080/greeting?name=#.

    And it will respond with a JSON representation of a greeting.

    1 创建新项目

    在这里插入图片描述

    我还引入了 lombok 依赖。

    2 创建资源表示类

    src/main/java/com/example/restservice/Greeting.java

    package com.example.restservice;
    import lombok.Data;
    @Data
    public class Greeting {
        private final long id;
        private final String content;
    
        public Greeting(long id, String content) {
            this.id = id;
            this.content = content;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    此应用程序使用使用 Jackson JSON 库自动地将类型Greeting的实例封装成JSON,网络启动器默认包含Jackson。

    3 创建资源控制器

    In Spring’s approach to building RESTful web service, HTTP requests are handled by a controller.

    src/main/java/com/example/restservice/GreetingController.java

    package com.example.restservice;
    
    import java.util.concurrent.atomic.AtomicLong;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class GreetingController {
    
        private static final String template = "Hello, %s!";
        private final AtomicLong counter = new AtomicLong();
    
        @GetMapping("/greeting")
        public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
            return new Greeting(counter.incrementAndGet(), String.format(template, name));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (1) @GetMapping 注释可以确保 HTTP GET 请求到的 /greeting 被映射到 greeting() 方法。

    (2) @RequestParam 将查询字符串参数name的值绑定到greeting()方法中的参数name。如果请求中不存在参数name,则使用 defaultValue 中的值 “World"。

    (3) 线程安全的AtomicXXX,适合用于多线程环境。.incrementAndGet()自增+1

    (4) @RestController 使这个类作为一个使所有方法返回域对象而不是视图的一个控制器。它包含 @Controller 和 @ResponseBody

    4 运行

    package com.example.restservice;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class RestServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(RestServiceApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    企业级npm仓库搭建
    如何基于 GORM 实现 CreateOrUpdate 方法
    Python pip更新教程(两种方式)
    FFplay文档解读-43-视频过滤器十八
    LCR 056.两数之和 IV
    安装mqtt服务器问题及处理办法
    32 随机链表的复制
    CSS 中的变量
    第十章 Linux系统安全及应用
    上半年巨亏51亿,小鹏汽车“掉队”
  • 原文地址:https://blog.csdn.net/qq_52077925/article/details/127782255