Java fresh business platform - unified format returned API architecture design and actual combat

Java fresh business platform - unified format returned API architecture design and actual combat

Description: With the popularity of the Internet all posts meticulous division of labor, there has been a lot of system architecture design, such as common before and after the end of the separation architecture, backend provides an interface to the front-end, front-end rendering the data interface, everyone armed with their job, efficiency is very high, but with the increase in the interface, if not uniform standard will increase the number of additional communication costs and learning costs, in terms of the manager is very unfavorable. For this reason, I share in this article give you a Java fresh business platform in how to return the uniform format of the overall architecture of the API.

 

FIG substantially overall architecture of general system is as follows:

It should be noted that some small partners will respond to say, this architecture too simple right, too low, what Gateway ah, ah caching, messaging middleware, ah, no. Because of this old Gu focuses on the API interface, so our focal point, other modules to supplement their own little friends.

Interactive interface

Front and rear ends interacts, in accordance with the front end of the appointment request URL path, and passing parameters, back-end server receives the request, performs service processing and returns the data to the front end.

For restful style URL path, and arguments passed requiring public request header (eg: app_version, api_version, device, etc.), the old Gu is not presented here, little friends can get to know themselves, it is relatively simple.

How to achieve back-end server to return data to the front end?

Return form

We generally returned to the front end of the rear end of a body JSON embodiment, is defined as follows:

{  #返回状态码  code:integer,      #返回信息描述  message:string,  #返回值  data:object}

CODE status code

code returns a status code, what is needed are generally small partners in the development of what is added. To return to the interface as user rights abnormal, we add a status code of 101 bar, next time have to add a parameter data anomalies, we add a 102 status code. So even though it can meet business as usual, but status code is too messy, we should refer to the HTTP request return status code

: The following HTTP status codes are common: 
200-- request was successful
301-- resource (web pages, etc.) are transferred permanently to other the URL
404 - Resource request (page) exists
500 - Internal Server Error

我们可以参考这样的设计,这样的好处就把错误类型归类到某个区间内,如果区间不够,可以设计成4位数。

#1000~1999 区间表示参数错误
#2000~2999 区间表示用户错误
#3000~3999 区间表示接口异常

这样前端开发人员在得到返回值后,根据状态码就可以知道,大概什么错误,再根据message相关的信息描述,可以快速定位。关注微信公众号 Java后端 获取更多推送。Message这个字段相对理解比较简单,就是发生错误时,如何友好的进行提示。一般的设计是和code状态码一起设计,如

再在枚举中定义,状态码

状态码和信息就会一一对应,比较好维护。Data返回数据体,JSON格式,根据不同的业务又不同的JSON体。我们要设计一个返回体类Result

控制层Controller

我们会在controller层处理业务请求,并返回给前端,以order订单为例

我们看到在获得order对象之后,我们是用的Result构造方法进行包装赋值,然后进行返回。小伙伴们有没有发现,构造方法这样的包装是不是很麻烦,我们可以优化一下。关注微信公众号 Java后端 获取更多推送。美观美化我们可以在Result类中,加入静态方法,一看就懂

那我们来改造一下Controller

代码是不是比较简洁了,也美观了。优雅优化上面我们看到在Result类中增加了静态方法,使得业务处理代码简洁了。但小伙伴们有没有发现这样有几个问题:

1、每个方法的返回都是Result封装对象,没有业务含义2、在业务代码中,成功的时候我们调用Result.success,异常错误调用Result.failure。是不是很多余3、上面的代码,判断id是否为null,其实我们可以使用hibernate validate做校验,没有必要在方法体中做判断。

我们最好的方式直接返回真实业务对象,最好不要改变之前的业务方式,如下图

这个和我们平时的代码是一样的,非常直观,直接返回order对象,这样是不是很完美。那实现方案是什么呢?

实现方案

小伙伴们怎么去实现是不是有点思路,在这个过程中,我们需要做几个事情

1、定义一个注解@ResponseResult,表示这个接口返回的值需要包装一下2、拦截请求,判断此请求是否需要被@ResponseResult注解3、核心步骤就是实现接口ResponseBodyAdvice和@ControllerAdvice,判断是否需要包装返回值,如果需要,就把Controller接口的返回值进行重写。

注解类用来标记方法的返回值,是否需要包装

拦截器

拦截请求,是否此请求返回的值需要包装,其实就是运行的时候,解析@ResponseResult注解

此代码核心思想,就是获取此请求,是否需要返回值包装,设置一个属性标记。重写返回体

上面代码就是判断是否需要返回值包装,如果需要就直接包装。这里我们只处理了正常成功的包装,如果方法体报异常怎么办?处理异常也比较简单,只要判断body是否为异常类。

怎么做全局的异常处理,篇幅原因,老顾这里就不做介绍了,只要思路理清楚了,自行改造就行。

重写Controller

在控制器类上或者方法体上加上@ResponseResult注解,这样就ok了,简单吧。到此返回的设计思路完成,是不是又简洁,又优雅。这个方案还有没有别的优化空间,当然是有的。如:每次请求都要反射一下,获取请求的方法是否需要包装,其实可以做个缓存,不需要每次都需要解析。当然整体思路了解,小伙伴们就可以在此基础上面自行扩展,如有收获,请帮忙转发,您的鼓励是作者最大的动力.

 

联系QQ:137071249

QQ群:793305035

Guess you like

Origin www.cnblogs.com/jurendage/p/11982149.html