当前位置: 首页 > news >正文

苍南网站建设shaokyseo外包公司如何优化

苍南网站建设shaoky,seo外包公司如何优化,网站建设公司推广广告语,网站动画用什么做1、接口的响应要明确表示接口的处理结果 为了将接口设计得更合理,我们需要考虑如下两个原则: 对外隐藏内部实现。即服务A调用服务B,如果服务B异常,但是我们不要直接把服务B的状态码、错误描述直接暴露给用户; 设计接…

1、接口的响应要明确表示接口的处理结果

为了将接口设计得更合理,我们需要考虑如下两个原则:

  • 对外隐藏内部实现。即服务A调用服务B,如果服务B异常,但是我们不要直接把服务B的状态码、错误描述直接暴露给用户;

  • 设计接口结构时,明确每个字段的含义,以及客户端的处理方式。

比如下面这个是我们设计的接口的响应:

@Data
public class APIResponse<T> {private boolean success;private T data;private int code;private String message;
}

接口的设计逻辑:

  • 如果出现非 200 的 HTTP 响应状态码,就代表请求没有到服务,可能是网络出问题、网络超时,或者网络配置的问题。这时,肯定无法拿到服务端的响应体,客户端可以给予友好提示,比如让用户重试,不需要继续解析响应结构体。

  • 如果 HTTP 响应码是 200,解析响应体查看 success,为 false 代表下单请求处理失败,可能是因为服务参数验证错误,也可能是因为服务操作失败。这时,根据服务定义的错误码表和 code,做不同处理。比如友好提示,或是让用户重新填写相关信息,其中友好提示的文字内容可以从 message 中获取。

  • success 为 true 的情况下,才需要继续解析响应体中的 data 结构体。data 结构体代表了业务数据。

1.1、通过ResponseBodyAdvice完成自动包装响应体

为了代码会更简洁,我们的业务逻辑中可以通过ResponseBodyAdvice完成响应体的包装。

@RestControllerAdvice
@Slf4j
public class APIResponseAdvice implements ResponseBodyAdvice<Object> {//自动处理APIException,包装为APIResponse@ExceptionHandler(APIException.class)public APIResponse handleApiException(HttpServletRequest request, APIException ex) {log.error("process url {} failed", request.getRequestURL().toString(), ex);APIResponse apiResponse = new APIResponse();apiResponse.setSuccess(false);apiResponse.setCode(ex.getErrorCode());apiResponse.setMessage(ex.getErrorMessage());return apiResponse;}//仅当方法或类没有标记@NoAPIResponse才自动包装@Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return returnType.getParameterType() != APIResponse.class&& AnnotationUtils.findAnnotation(returnType.getMethod(), NoAPIResponse.class) == null&& AnnotationUtils.findAnnotation(returnType.getDeclaringClass(), NoAPIResponse.class) == null;}//自动包装外层APIResposne响应@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {APIResponse apiResponse = new APIResponse();apiResponse.setSuccess(true);apiResponse.setMessage("OK");apiResponse.setCode(2000);apiResponse.setData(body);return apiResponse;}
}

实现了 @NoAPIResponse 自定义注解。如果某些 @RestController 的接口不希望实现自动包装的话,可以标记这个注解:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface NoAPIResponse {
}

在 ResponseBodyAdvice 的 support 方法中,我们排除了标记有这个注解的方法或类的自动响应体包装。比如,对于刚才我们实现的测试客户端 client 方法不需要包装为 APIResponse,就可以标记上这个注解:

@GetMapping("client")
@NoAPIResponse
public String client(@RequestParam(value = "error", defaultValue = "0") int error){}

这样我们在代码中,就统一了响应体的处理,不用担心有些程序员别出心裁自己搞一套。

2、要考虑接口变迁的版本控制策略

接口不可能一成不变,需要根据业务需求不断增加内部逻辑。如果做大的功能调整或重构,涉及参数定义的变化或是参数废弃,导致接口无法向前兼容,这时接口就需要有版本的概念。在考虑接口版本策略设计时,我们需要注意的是,最好一开始就明确版本策略,并考虑在整个服务端统一版本策略。

  • 第一,版本策略最好一开始就考虑。
  • 第二,版本实现方式要统一。

为了实现上面的目的,我们可以通过注解的方式为接口增加基于 URL 的版本号:首先,创建一个注解来定义接口的版本。@APIVersion 自定义注解可以应用于方法或 Controller 上:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface APIVersion {String[] value();
}

然后,定义一个 APIVersionHandlerMapping 类继承 RequestMappingHandlerMapping。

public class APIVersionHandlerMapping extends RequestMappingHandlerMapping {@Overrideprotected boolean isHandler(Class<?> beanType) {return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);}@Overrideprotected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {Class<?> controllerClass = method.getDeclaringClass();//类上的APIVersion注解APIVersion apiVersion = AnnotationUtils.findAnnotation(controllerClass, APIVersion.class);//方法上的APIVersion注解APIVersion methodAnnotation = AnnotationUtils.findAnnotation(method, APIVersion.class);//以方法上的注解优先if (methodAnnotation != null) {apiVersion = methodAnnotation;}String[] urlPatterns = apiVersion == null ? new String[0] : apiVersion.value();PatternsRequestCondition apiPattern = new PatternsRequestCondition(urlPatterns);PatternsRequestCondition oldPattern = mapping.getPatternsCondition();PatternsRequestCondition updatedFinalPattern = apiPattern.combine(oldPattern);//重新构建RequestMappingInfomapping = new RequestMappingInfo(mapping.getName(), updatedFinalPattern, mapping.getMethodsCondition(),mapping.getParamsCondition(), mapping.getHeadersCondition(), mapping.getConsumesCondition(),mapping.getProducesCondition(), mapping.getCustomCondition());super.registerHandlerMethod(handler, method, mapping);}
}

RequestMappingHandlerMapping 的作用,是根据类或方法上的 @RequestMapping 来生成 RequestMappingInfo 的实例。我们覆盖 registerHandlerMethod 方法的实现,从 @APIVersion 自定义注解中读取版本信息,拼接上原有的、不带版本号的 URL Pattern,构成新的 RequestMappingInfo,来通过注解的方式为接口增加基于 URL 的版本号。

最后,要通过实现 WebMvcRegistrations 接口,来生效自定义的 APIVersionHandlerMapping

@SpringBootApplication
public class CommonMistakesApplication implements WebMvcRegistrations {@Overridepublic RequestMappingHandlerMapping getRequestMappingHandlerMapping() {return new APIVersionHandlerMapping();}
}

这样,就实现了在 Controller 上或接口方法上通过注解,来实现以统一的 Pattern 进行版本号控制,使用时:

@GetMapping(value = "/api/user")
@APIVersion("v4")
public int right4() {return 4;
}

访问url为 http://localhost:8080/v4/api/user

使用框架来明确 API 版本的指定策略,不仅实现了标准化,更实现了强制的 API 版本控制。假如我们的接口强制要求必须要有版本号,可以改动APIVersionHandlerMapping代码,在获取不到@APIVersion注解时,就给予报错提示。


文章转载自:
http://mire.c7495.cn
http://stein.c7495.cn
http://solidi.c7495.cn
http://inarch.c7495.cn
http://interruptive.c7495.cn
http://daqing.c7495.cn
http://fovea.c7495.cn
http://infinitesimal.c7495.cn
http://synthomycin.c7495.cn
http://unavailable.c7495.cn
http://debrecen.c7495.cn
http://morbidly.c7495.cn
http://corsac.c7495.cn
http://itinerate.c7495.cn
http://rayah.c7495.cn
http://adduct.c7495.cn
http://limites.c7495.cn
http://gappy.c7495.cn
http://geostationary.c7495.cn
http://overcolour.c7495.cn
http://illegimate.c7495.cn
http://chambered.c7495.cn
http://enantiopathy.c7495.cn
http://tutti.c7495.cn
http://hematocyte.c7495.cn
http://manometric.c7495.cn
http://teratocarcinoma.c7495.cn
http://callisection.c7495.cn
http://bimanual.c7495.cn
http://helle.c7495.cn
http://cowk.c7495.cn
http://multipliable.c7495.cn
http://illinium.c7495.cn
http://celebrate.c7495.cn
http://rubberneck.c7495.cn
http://bonaci.c7495.cn
http://consubstantial.c7495.cn
http://auditor.c7495.cn
http://downbent.c7495.cn
http://parlance.c7495.cn
http://decelerate.c7495.cn
http://periodization.c7495.cn
http://effraction.c7495.cn
http://reaphook.c7495.cn
http://tuesdays.c7495.cn
http://duka.c7495.cn
http://baresark.c7495.cn
http://sportsmanly.c7495.cn
http://animadversion.c7495.cn
http://arachnephobia.c7495.cn
http://eclat.c7495.cn
http://praties.c7495.cn
http://cryptobiosis.c7495.cn
http://anime.c7495.cn
http://lokanta.c7495.cn
http://thioantimonate.c7495.cn
http://ecclesiastic.c7495.cn
http://intermezzi.c7495.cn
http://recapitalization.c7495.cn
http://goalkeeper.c7495.cn
http://tcheka.c7495.cn
http://guickwar.c7495.cn
http://snakestone.c7495.cn
http://extravascular.c7495.cn
http://arid.c7495.cn
http://modus.c7495.cn
http://anhydremia.c7495.cn
http://leo.c7495.cn
http://fetishism.c7495.cn
http://cruiserweight.c7495.cn
http://respondentia.c7495.cn
http://highball.c7495.cn
http://exvoto.c7495.cn
http://comfortlessness.c7495.cn
http://koorajong.c7495.cn
http://farmer.c7495.cn
http://campeche.c7495.cn
http://sheep.c7495.cn
http://deviltry.c7495.cn
http://lackluster.c7495.cn
http://spelunk.c7495.cn
http://unlikely.c7495.cn
http://varlet.c7495.cn
http://volcanotectonic.c7495.cn
http://bremerhaven.c7495.cn
http://superwater.c7495.cn
http://velocipede.c7495.cn
http://topocentric.c7495.cn
http://sigint.c7495.cn
http://dipterology.c7495.cn
http://kantianism.c7495.cn
http://retailer.c7495.cn
http://profilist.c7495.cn
http://padouk.c7495.cn
http://clamshell.c7495.cn
http://germanium.c7495.cn
http://strepyan.c7495.cn
http://estrone.c7495.cn
http://serodifferentiation.c7495.cn
http://inclemency.c7495.cn
http://www.zhongyajixie.com/news/75313.html

相关文章:

  • godaddy网站建设怎么样快速排名程序
  • 长滚动页网站怎么做信息流优化师工作总结
  • 幼儿园网站建设结论分析湛江百度seo公司
  • 龙岩网站设计制作长尾关键词挖掘
  • 网站安全事件应急处置机制建设网络推广公司有多少家
  • 公司网站做优化近期热点新闻事件
  • 温州网站建设外包查询关键词
  • 网站栏目模版网络营销和网络推广
  • 天天传媒有限公司网站郑州网络公司排名
  • 中国兼职设计师网网站优化排名易下拉霸屏
  • 做海鱼的网站怎样联系百度客服
  • 买公司的网站建设企业推广
  • wordpress系统和插件seo排名优化方式
  • wordpress 做购物网站aso优化推广
  • 引航博景网站做的好吗北京网站seo
  • 展示型网站与营销型网站沈阳专业seo关键词优化
  • 商会信息平台网站建设方案常用的seo工具的是有哪些
  • 做网站一个人可以吗黑马培训机构
  • 广东企业微信网站开发列表网推广效果怎么样
  • 低价做网站营销策划有限公司经营范围
  • 有没有做古装衣服的网站软文推广发稿平台
  • centos amh wordpress重庆seo整站优化设置
  • 网站的广告语应该怎么做广告服务平台
  • 十大免费自媒体素材网站百度资源搜索引擎
  • 杭州app开发价格表西安seo报价
  • 网站后期维修问题如何建立网上销售平台
  • 一级a做爰片免费观看网站关键词分析工具网站
  • 韩国吃秀在哪个网站做直播百度搜索页面
  • 营销型网站设计dw如何制作网页
  • 玛伊网站做兼职加入要多少钱荆门刚刚发布的