几个增加 RESTful API 设计的可用性的技巧

Springboot3新特性异常信息ProblemDetail详解 例如,考虑一个响应,该响应表明客户的账户没有足够的权限。此外,Problem Detail信息可以包含其他信息,例如标识问题具体发生的URI(有效地为“Joe上周四没有足够的信用”这一概念提供了标识符),这对于支持或取证目的可能很有用。,以确保这个“properties”映射被展开,并在响应中作为顶级JSON属性呈现,同样,反序列化期间的任何未知属性都会插入到这个Map中。的方便基类,它处理所有Spring MVC异常,以及任何ErrorResponseException,并渲染一个带有主体的错误响应。 阅读详情

Some Techniques for Better Usability in REST API Design

Someone said that API designers are UX engineers for developers. Good API design should be easy to use for consumers as well as easy to be implemented by developers.

Here are some techniques I summarized from my API design tasks. Let me know if it is helpful or you have more ideas on how to design a easy-to-use API.

Return 200 OK with Empty Array or 404 Not Found?

Problem

Sometimes, it is hard to tell if a GET API should return 200 OK with an empty array or it should return 404 not found. Actually, the answer is how the API client would like to handle it.

A 4XX response usually indicate there is something wrong in client’s request. Client also will treat it with a customized error page. Before an API designer makes a decision, it’s better to ask client developers how they will use the response.

Technique

However, there are common practice on which status code we should use. A API should return 200 OK with empty array if the purpose is to search records for a player or other criteria since an empty array isn’t an error caused by the client. It just means there isn’t any records of the player. However, the API should return 404 if the player cannot be found in the system because the client may entered a wrong player ID and the system should prompt this error to the user.

Example

Here is an example. In this example, the system tries to search active user sessions of a station. It returns 200 OK with an empty array while it returns 404 not found if station ID cannot be found.
在这里插入图片描述

Use controller or Use PUT to modify a resource?

Problem

In RESTful APIs, PUT is suggested to modify a resource but we also see APIs use controller with a POST for resource modification. Which way is better?

When you use put, you use body to wrap the object to be modified but the issue is we only need to change a status of an object, which is minimal change. You may confuse a developer in such case if you transfer an entire object to service for this purpose.

Technique

Use controller to minimize the number of parameters if you only want to change a status.

Example

You can see the difference between changing status of CPV (chip purchase voucher) with PUT and Controller.

PUT

The following images show the API use put to change a CPV status to redeemed and voided. The request body is CPVInfo object but you can see the only mattered properties are CPV ID, CPV status, and transaction info among all the properties. The API designer needs to tell developers which properties should be used in the API. This lowers usability of the API. The good news is that this make it easier to see transaction details if we need to analyze logs because the request body includes all the detail information of the CPV. We don’t need to reference other information source such as database.
在这里插入图片描述
在这里插入图片描述
Controller

The following example uses redeem and void controllers to redeem and void a CPV. You can see only transaction information is needed in request body. This makes much clear to API developers what parameters are actually should be used in the implementation. There isn’t CPV related parameters except CPV ID.

请添加图片描述

Use Read Only to specify properties that only should be shown in a response

Problem

Designers always want to reuse schemas to save their effort. For the above example, a CPVInfo object definition is used in both issue CPV API and redeem/void CPV API but not all properties of CPVInfo object are used in said APIs. Some properties should only be used in request and some properties are supposed to be used in response. How can API designers describe the usage scenario in this case?

Technique

In open API specification, you can mark a property as ReadOnly, which means this property shouldn’t be used in request and it will only be shown in response.

Example

In the following example, I mark CPVInfo.IssuanceTime as ReadOnly since it is generated when a CPV was issued at service side. It’s not required when client tries to issue a CPV. As you can see, it won’t appear in issue CPV API request.

在这里插入图片描述
在这里插入图片描述
IssuanceTime is marked as ReadOnly in CPVInfo schema.
在这里插入图片描述

Use Problem Details to show errors to client

Problem

For API errors, we often see different products use different error object definition. Actually, there is an industry standard for RESTful API error object definition. It is also recommended in REST API guideline and adopted by .Net Core.

Technique

Use ProblemDetails to describe API errors. It is defined in https://tools.ietf.org/html/rfc7807. It’s easier for your API consumer to understand and use your error description using industry standard. They can easily find documents of it online.

Example
在这里插入图片描述

Use sub path for sub transaction types

Problem

We need to use many words to document an API usage if this API has a few special cases or sub types. In most cases, our user cannot use the API correctly without reading the document. For PA engineers, it also takes time to test all the special cases or sub types if they are packaged in one API. Any modification to the API causes a full regression.

Technique

We can use sub path for special cases or sub types under the API so that user can learn all these cases and sub types by just reading the API path and testers can test the API cases one by one without worrying a chained error effect when there is a modification.

Example

Zero Play Rating

The following request is used to add a table rating to CMS. Users need to fill quite a few parameters to post a rating. This works in most cases but there is a special case which is that a player doesn’t play. We call it Zero Play. To post a Zero Play rating, users need to figure out Zero Play parameters pattern, which will take some time.

The solution to it is to add a sub path to the existing Post Rating API. For this example, we will change /api/v1/table-ratings to /api/v1/table-ratings/zero-play and remove the unnecessary parameters from the request. In this way, users can know how to post a Zero Play rating just from the API path.

{
  "transactionInfo": {
    "transactionTime": "2020-07-08T01:18:07.0623569+00:00",
    "postedBy": "*****",
    "verifiedBy": "*****",
    "enteredBy": "******",
    "workstation": "123"
  },
  "ratingId": 38020,
  "sourceId": "1",
  "sourceName": "test table",
  "playerId": 125,
  "programId": 0,
  "tableID": "LA01BJ01",
  "gameType": "BJ",
  "gameName": "BlackJackBJ",
  "gamingDate": "2020-07-08T01:18:07.0623888+00:00",
  "shift": "1",
  "pitID": 1,
  "pitName": "Poker 1",
  "position": 2,
  "row": 1,
  "startTime": "2020-07-08T01:16:07.0624008+00:00",
  "endTime": "2020-07-08T01:18:07.06241+00:00",
  "timePlayed": 500,
  "averageBet": 544,
  "speed": 49,
  "handsPlayed": 6,
  "cashIn": 3951,
  "chipsIn": 1078,
  "markersIn": 1287,
  "moneyPlays": 1424,
  "chipsOut": 2908,
  "ratingIns": [
    {
      "code": "1",
      "description": "bla in",
      "amount": 2528,
      "cashEquivalent": false,
      "displayOrdinal": 0
    },
    {
      "code": "CashIn",
      "description": "Cash In",
      "amount": 15255,
      "cashEquivalent": false,
      "displayOrdinal": 0
    },
    {
      "code": "CashlessIn",
      "description": "Cashless In",
      "amount": 2589,
      "cashEquivalent": false,
      "displayOrdinal": 0
    }
  ],
  "ratingOuts": [
    {
      "code": "2",
      "description": "bla out",
      "amount": 1766,
      "cashEquivalent": false,
      "displayOrdinal": 0
    },
    {
      "code": "ChipsOut",
      "description": "Chips Out",
      "amount": 6582,
      "cashEquivalent": false,
      "displayOrdinal": 0
    },
    {
      "code": "CPVRedeem",
      "description": "CPV Redeem",
      "amount": 5430,
      "cashEquivalent": false,
      "displayOrdinal": 0
    }
  ],
  "netBuyIn": 390,
  "winLoss": 743,
  "skill": "Average",
  "ratingComments": "test comment",
  "skillFactor": 4,
  "dealer": "******",
  "marker": [
    {
      "documentId": 198273,
      "amount": 10
    }
  ]
}
如何设计一个高并发的RESTful API? 高并发RESTful API的设计是理论与实践的结合,需从第一性原理出发,拆解请求处理的全链路,优化每个环节的效率。架构分层:接入层、网关层、服务层、数据层、缓存层的责任分离;异步处理:使用Spring WebFlux提高并发度;缓存策略:用Redis减少数据库查询;限流熔断:避免系统过载和雪崩效应;弹性伸缩:用K8s实现按需扩展。通过本文的指导,开发者可从入门级的概念理解,逐步掌握专家级的优化技巧,最终设计出“低延迟、高可用、可弹性”的高并发RESTful API。 阅读详情

相关推荐

restful api和普通api有什么特点_优秀API的设计原则与实例实现RESTful

API是服务之间通信的契约,通过API可以忽略服务内部实现的细节。如果接口设计良好,则可以降低团队间的耦合度,加快开发速度。优秀API的设计原则设计出优秀的API通常需要遵循如下原则。·简单:API应该符合大多数人正常的思维逻辑,避免异想天开的交互,满足需求的同时,越简单越好。·易懂:优秀的API可读性好,尽量做到不需要文档就能读懂接口名称、参数的大概含义,提供给第三方开发者的接口要...

weixin_39637924的博客 1880

restful api设计_RESTful API设计

restful api设计 在编写API规范之前,必须考虑RESTful API规范的五个主要方面。 在本文中,我将使用产品用例的示例来讨论这五个功能。 在开始之前,请确保我们了解API和REST的含义。 如果您已经熟悉API设计并且想进一步学习,建议您看一下该教程: 如何使用API​​ Designer设计RESTful API 。 什么是API? 应用程序编程接口(API)是一组规则,...

cunfen8879的博客 264

RESTful API的版本管理、安全性、可用性、文档和测试、监控

REST (Representational State Transfer) 是一种架构风格,它定义了一组约束条件和原则。它主要用于客户端和服务器之间交换数据,通过一系列的REST API调用来实现服务。REST是基于HTTP协议标准化的一种Web服务接口标准。RESTful API是一种遵循REST规范的API。REST API的版本管理每个新版本的REST API都应该有其文档。RESTful API的版本管理可以有效防止API的过时或废弃,并让开发者清楚地知道自己在使用的哪个版本的API。

AI天才研究院 1297

重学SpringBoot3-Problemdetails

随着 Spring Boot 3 的发布,Spring Framework 6 继续在提升开发者体验和应用性能方面迈出重要步伐。在众多引人注目的新特性中,对 Problem Details(问题详情)的支持尤为值得关注。这一特性基于 RFC 7807 标准,旨在为 HTTP API 提供一种标准化的错误响应格式。本文将深入探讨 Spring Boot 3 中 Problem Details 的概念、应用及其对微服务架构的潜在影响。

CoderJia的学习之路 1820

Python Flask的RESTful API优化技巧

在当今的Web开发中,RESTful API已经成为了不同系统之间进行数据交互的主流方式。Python Flask作为一个轻量级的Web框架,因其简洁易用的特点,被广泛应用于快速开发RESTful API。然而,随着业务的不断发展和用户量的增加,API的性能和稳定性可能会面临挑战。本文的目的就是深入探讨Python Flask的RESTful API优化技巧,涵盖性能优化、代码优化、安全优化等多个方面,帮助开发者构建高效、稳定、安全的RESTful API。

AI Python 编程之道的博客 861

workbench拓扑优化教程_拓扑优化实例-轮毂的优化

拓扑优化实例-轮毂的优化ANSYS Workbench的新版本集成了topology optimization模块,为用户的拓扑优化提供的很好的应用。通过拓扑优化topology optimization模块的使用可以对拓扑有较好的理解,该模块拓扑优化可以简单概括为一句话:如何在合适的位置去除确定的材料使用量,而相应的刚度变化影响最小。本例以汽车轮毂为例,来确定轮毂的辐条形状。1.建立模型DM中可...

weixin_39581739的博客 3051

restFul的增删改查

Spring自定义注解学习:https://blog.csdn.net/qq_34582693/article/details/88943120 JVM底层原理学习:https://blog.csdn.net/qq_34582693/article/details/79513676 JAVA多线程学习:https://blog.csdn.net/qq_34582693/article/details/79843258 Spring框架学习:https://blog.csdn.net/qq_34582693/

Aa__Ki的博客 1579

论文浅读之PIP-Net: Patch-Based Intuitive Prototypes for Interpretable Image Classification

这篇文章的主要贡献包括:1 提出了一种新型的可解释图像分类模型PIP-Net,它基于原型部分学习可解释的原型,实现内置解释性。2 PIP-Net采用自监督学习的新正则化方法,学习原型之间的相似性,使得 latent 空间中的相似性能更好地与人类视觉感知相一致,从而减小了所谓的语义差距。3 PIP-Net可解释为一个稀疏的打分表,它可以检测图像是否属于任何类,或是否属于多个类。它也可以对OOD数据进行分类并说“我没有看到过这个”。

qq_49370210的博客 1797

Restful风格的五种基本操作

Restful风格的五种基本操作

lxrqiyuan的博客 782

AIDL HAL服务从零到一:构建现代Android硬件抽象层

本文详细介绍了从零开始构建现代Android AIDL HAL服务的完整流程。通过定义接口、实现服务逻辑、配置构建与VINTF清单,并最终集成到系统镜像,开发者可以掌握利用AIDL替代传统HIDL来开发硬件抽象层的核心方法。文章重点解析了AIDL HAL的创建流程,并对比了其与HIDL在语法、工具链和集成方式上的优势,帮助开发者高效构建稳定的硬件接口。

weixin_29174141的博客 150

ArduSub程序学习(1)——ardupilot框架整理

AP_HAL层(硬件抽象层)是我们制作 ArduPilot 的方式可移植到许多不同的平台。有一个顶级AP_HAL libraries/AP_HAL,用于定义代码其余部分的接口 有针对具体的板子特性,然后就有了AP_HAL_XXX 子目录,例如,AP_HAL_AVR 用于基于 AVR 的 开发板,AP_HAL_PX4用于 Pixhawk 开发板和 AP_HAL_Linux 基于 Linux 开发板。工具目录tools directories工具目录是杂项支持目录。

weixin_49839886的博客 2823

【RESTful】RESTful API 接口设计规范 | 示例

概念 本质:一种软件架构风格 核心:面向资源设计的API 解决问题: 降低开发的复杂性 提高系统的可伸缩性 例如:设计一套API,为多个终端服务。 设计概念和准则 网络上的所有事物都可以被抽象为资源 每一个资源都有唯一的资源标识,对资源的操作不会改变这些标识 所有的操作都是无状态的(本次操作、下次操作、上次操作之间无关系) 资源:网络上的一个实体、具体信息。 HTTP RESTful 与...

前端进阶之路 | 中大厂、外企、国企内推 | 面试培训 | 简历修改 2万+

告别Overleaf!本地LaTeX环境配置:VSCode+TeXLive完全指南(Windows/Mac双平台)

本文详细介绍了如何在Windows和Mac平台上配置本地LaTeX环境,使用VSCode和TeXLive打造高效的专业写作工作流。从TeXLive安装到VSCode插件配置,再到高级技巧与性能优化,帮助用户摆脱Overleaf限制,实现更快速、更私密的学术写作体验。

3c4x5z6v7b的博客 962

RESTful API接口基本内容

RESTful API接口基本内容 目录 RESTful API接口基本内容 一、RESTful是什么? 1.1.基本特点 1.2.设计概念和准则 1.3.http协议 二、RESTful与SOAP WebService 架构有什么区别? 2.1.WebService 架构概念 2.2.两者对比 三、如何实现符合RESTful规范的API? 3.1.资源路径(URI) 3....

qq_32938957的博客 2万+

解决Todesk连接卡在100%问题:remote display server is not supported (e.g. Wayland)

摘要:解决Ubuntu 22.04远程桌面连接问题(AnyDesk报错"remote display server not supported"及向日葵/Todesk无法显示)。方法:1)安装lightdm显示管理器;2)启用lightdm并禁用gdm;3)编辑/etc/gdm3/custom.conf文件,添加WaylandEnable=false;4)重启系统。注:后续可通过命令切换回gdm显示管理器。该方法适用于解决Wayland显示协议导致的远程桌面连接兼容性问题。

猿小白的博客 1584

YOLO11涨点优化:注意力魔改 | 新颖的多尺度卷积注意力(MSCA),即插即用,助力小目标检测

多尺度卷积注意力(MSCA),有效地提取上下文信息,新颖度高,创新十足。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 6341

RESTful API 设计规范详解

客户端的每一次请求,服务器都必须给出回应。回应包括 HTTP 状态码和数据两部分。HTTP 状态码就是一个三位数,分成五个类别。1xx: 相关信息2xx: 操作成功3xx: 重定向4xx: 客户端错误5xx: 服务器错误这五大类总共包含100多种状态码,覆盖了绝大部分可能遇到的情况。每一种状态码都有标准的(或者约定的)解释,客户端只需查看状态码,就可以判断出发生了什么情况,所以服务器应该返回尽可能精确的状态码。

Flag少侠的博客 1万+

【MATLAB】工业传送带速度闭环控制与张力联合仿真

本文基于MATLAB/Simulink平台,构建了工业传送带速度闭环控制与张力联合仿真系统。通过搭建驱动模块、机械传动模块、检测模块和控制模块,实现了速度与张力的协同控制。采用双PID调节器分别整定速度与张力参数,经联合调试后系统性能良好:速度跟踪误差小于±0.03m/s,张力波动控制在±30N内,负载扰动恢复时间仅0.3秒。研究提供了从建模到参数优化的完整工程方案,可直接应用于课程设计或工业实践,有效解决了传送带运行中速度偏差与张力波动的协同控制难题。

unber的博客 83

【RESTful】RESTful API的设计原则

设计一套高质量的RESTful API需要遵循一系列原则,以确保其可用性和可维护性。良好的API设计不仅能提升开发效率,还能改善用户体验。随着技术的不断进步,开发者应持续探索和优化这些设计原则,以适应不断变化的需求。本文旨在帮助读者深入理解RESTful API的设计原则,并提供实用的指导,以便在项目中有效应用这些原则。希望这些见解能为您的API设计提供帮助,助力您的开发工作更加顺利。

人生苦短,睡觉要紧,睡醒再说 3068

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

RESTful风格API详解

在学习RESTful 风格接口之前,即使你不知道它是什么,但你肯定会好奇它能解决什么问题?有什么应用场景?听完下面描述我想你就会明白: 在互联网并没有完全流行的初期,移动端也没有那么盛行,页面请求和并发量也不高,那时候人们对接口的要求没那么高,一些动态页面(jsp)就能满足绝大多数的使用需求。 但是随着互联网和移动设备的发展,人们对Web应用的使用需求也增加,传统的动态页面由于低效率而渐渐被HTML+JavaScript(Ajax)的前后端分离所取代,并且安卓、IOS、小程序等形式客户端层出不穷,客户端的

tc979907461的博客 4620

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

RESTful API设计

一、RESTful API简介 1.1 RESTfulAPI是什么 RESTful API全称是Resource Representational State Transfer(资源表述性状态转移)。 R:Resource 资源。例如:user、role、gist、issue、service、route E:Representational 表述性。数据的表现形式,例如:JSON、XML、CSV、IMG等 S:State 状态。资源的状态,例如:创建、更新等状态 T:Transfer 转移.

Just Do It 972

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Restful API 设计原则

Restful API 设计原则 前言 网络应用程序分为前端和后端两个部分。 当前的发展趋势,就是前端设备层出不穷(手机、平板、桌面电脑、其他专用设备……),这也就意味着前端的开发代码、开发框架变得多种多样。因此,必须有一种统一的机制,方便不同的前端代码与后端进行通信。 这就导致了API构架的流行。REST是目前比较成熟的一套互联网应用程序的API设计理论。 它可以降低开发的复杂度,提...

Mr.Phoebe的专栏 7666

Restful API

RESTful API是一种遵循REST架构风格的API设计规范,其核心思想是将网络资源抽象化并通过HTTP动词进行操作。主要特点包括:URI代表资源(使用复数名词)、HTTP方法(GET/POST/PUT/DELETE等)对应操作、统一状态码和响应格式。相较于传统面向操作的API设计,RESTful风格将关注点转向资源本身,使用统一的资源路径配合不同HTTP方法实现CRUD操作,使系统更加清晰规范。设计要点还包含版本控制、过滤参数和超媒体API等扩展能力。这种设计风格提高了API的可读性、可维护性和扩展性

LYB的博客 1119

restful api接口设计

技术由来: 互联网早期,页面请求和并发量不高,且移动端未盛行时对接口要求不高,使用动态页面(jsp)就能满足绝大多数的使用需求。但是随着互联网和移动设备的发展,人们对Web应用的使用需求也增加,传统的动态页面由于低效率而渐渐被HTML+JavaScript(Ajax)的前后端分离所取代,并且安卓、IOS、小程序等形式客户端层出不穷,客户端的种类出现多元化,而客户端和服务端就需要接口进行通信,但接口的规范性就又成了一个问题: 所以一套结构清晰、符合标准、易于理解、扩展方便让大部分人都能够理解接受的接口

zhouzuoluo的博客 1248

一文搞懂什么是RESTful API

在具体构建RESTful API的时候,需要对各种请求有更细致的认知,当然,本案例在实现各种请求的时候为了演示的便捷并没有完全遵循RESTful API规范,例如版本号等信息这里就不添加了,案例更侧重于使用SpringBoot实现这个接口。到这里RESTful API的介绍和实战就结束啦,本篇首先从RESTful的一些特点进行介绍,再到SpringBoot实战RESTful API,最后也说了一些RESTful API并不完美的地方,相信睿智的你对RESTful 一定有了很深刻的理解。

weixin_69619960的博客 2008

RESTful API 设计最佳实践(8)

前面几篇文章介绍了一些RESTful API设计方面的参考规范,这篇文章我们来看几个“不良”的例子,并以个人的实践总结为这一系列做个结尾。欢迎大家评论交流。一、几个“不良”示例 1. 查询当前活动状态 ** 请求:** GET /activity/query?method=status&game=PDK&type=a Accept:json ** 应答:** {:status-code 200

蜗牛的专栏 787
上一篇: 如何订阅 ROS Float32MultiArray 消息类型
下一篇: 如何把asp.net core服务做成为Windows Service服务
surfirst
博客等级 码龄25年 5118粉丝 342原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

surfirst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值