You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
go 工程师离职, 后端只有 java 工程师的情况下,将此接口改用 java 重写一遍,但传参方式不变(query key 还是为中文)
实现
可选方案
方案一:不用 java 改写,java 工程师维护 golang 项目
优点:代码改动最小,出现 bug 的概率也最小
缺点:需要花时间学习 go 语言和相关项目
方案二:获取 HttpServletRequset 对象, 在相应的接口入口层进行参数处理
优点:能够获取到所有参数,快速实现功能
缺点:针对特定接口实现,不利于代码扩展
方案三:自定义 MVC 层参数解析器解析请求参数并绑定
优点:能够抽取此类需求的公共参数,结合反射和注解机制利于相似需求的扩展
缺点:相对于方案二性能可能会差一点。
涉及到反射处理
方案三实现
MVC 方法参数处理器接口 HandlerMethodArgumentResolver
publicinterfaceHandlerMethodArgumentResolver {
/** * 此解析器是否支持该方法参数的解析 */booleansupportsParameter(MethodParameterparameter);
/** * 将拿到的原始数据解析成想要的参数对象 * A {@link ModelAndViewContainer} provides access to the model for the * request. A {@link WebDataBinderFactory} provides a way to create * a {@link WebDataBinder} instance when needed for data binding and * type conversion purposes. * @param parameter the method parameter to resolve. This parameter must * have previously been passed to {@link #supportsParameter} which must * have returned {@code true}. * @param mavContainer the ModelAndViewContainer for the current request * @param webRequest the current request * @param binderFactory a factory for creating {@link WebDataBinder} instances * @return the resolved argument value, or {@code null} if not resolvable * @throws Exception in case of errors with the preparation of argument values */@NullableObjectresolveArgument(MethodParameterparameter, @NullableModelAndViewContainermavContainer,
NativeWebRequestwebRequest, @NullableWebDataBinderFactorybinderFactory) throwsException;
}
背景
实现
可选方案
方案三实现
总结
涉及知识点
遇到的问题
父类方法反射获取子类字段的取值(子类实例调用时)
spring doc openapi 参数转换器问题
存在的问题及可以继续改进的地方
参考及示例代码
代码:
custom_mvc_method_argument_resolver
参考:
Spring From the Trenches: Creating a Custom HandlerMethodArgumentResolver
mvc-ann-methods
oracle-reflect-fieldTypes
The text was updated successfully, but these errors were encountered: