问题描述
项目组小美同学接到一个商户平台提现到微信零钱的需求,由于该接口其他同事之前已经实现过,小美同学充分发挥了"拿来主义"精神.直接进行copy修改了一下相关名称之后就和前端小哥联调接口.问题就出现在"新接口"中写的请求参数明明是activityId,但是swagger接口文档中请求参数以及请求示例均为distributionId.原接口文档截图和现接口文档截图如下:
原controller:
@ApiOperation("提现到零钱")
@PostMapping("/transferAccount")
public ResultVo<TransferDetailQueryDto> transferAccount(@RequestBody @Validated TransferParamDto transferParamDto) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, IOException, KeyStoreException {
TransferDetailQueryDto transferDetailQueryDto = payService.transferAccount(transferParamDto);
return ResultVoUtil.success(transferDetailQueryDto);
}
现controller如下:
@ApiOperation("商户平台提现到零钱")
@PostMapping("/activityTransferAccount")
public ResultVo<TransferActivityDetailQueryDto> activityTransferAccount(@RequestBody @Validated TransferRequestParamDto transferRequestParamDto) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, IOException, KeyStoreException {
TransferActivityDetailQueryDto transferActivityDetailQueryDto = activityOrderService.transferAccount(transferRequestParamDto);
return ResultVoUtil.success(transferActivityDetailQueryDto);
}
两个方法中方法名、请求路径、请求参数对象均不同。
原请求参数:
@ApiModel("提现到零钱请求参数")
@Data
public class TransferParamDto implements Serializable {
private static final long serialVersionUID = -7913649087872041457L;
@NotBlank(message = "openId不能为空")
@ApiModelProperty(value = "用户在不同平台的openId,分销活动传递用户在咔哇小鱼课堂的openId",example = "olOCywVd7YyovEarBRAOAiQnggyY",dataType = "String")
private String openId;
@ApiModelProperty(value = "转账总金额,单位分",example = "1",dataType = "Integer")
@NotNull(message = "转账金额不能为空")
@Min(value = 1,message = "转账总金额不允许为0!")
private Integer transferAccount;
@ApiModelProperty(value = "分销活动id,1.99元分销活动",example = "1",dataType = "Integer")
@NotNull(message = "分销活动id不能为空")
@Min(value = 1,message = "分销活动id不允许为0!")
private Integer distributionId;
}
现请求参数如下:
@ApiModel("提现到零钱请求参数")
@Data
public class TransferRequestParamDto {
@NotBlank(message = "openId不能为空")
@ApiModelProperty(value = "用户在不同平台的openId",example = "olOCywVd7YyovEarBRAOAiQnggyY",dataType = "String")
private String openId;
@ApiModelProperty(value = "转账总金额,单位分",example = "1",dataType = "Integer")
@NotNull(message = "转账金额不能为空")
@Min(value = 1,message = "转账总金额不允许为0!")
private Integer transferAccount;
@ApiModelProperty(value = "活动id:1.24选6活动",example = "1",dataType = "Integer")
@NotNull(message = "活动id不能为空")
@Min(value = 1,message = "活动id不允许为0!")
private Integer activityId;
}
两个请求对象中仅activityId
和distributionId
不同,其余均相同。
发扬助人为乐的精神有空帮忙给她看看,本以为很简单的问题,愣是花费了一个小时才解决,特地记录一下,如果有相同情况的同学避免踩坑!
问题原因
简单看了一下写的方式,没有感觉哪个地方有问题。但是接口文档确实显示的不一致,没办法只能调源码一步一步看。最终发现问题出在
@ApiModel("提现到零钱请求参数")
ApiModel注解如下:
@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ApiModel {
/**
* Provide an alternative name for the model.
* <p>
* By default, the class name is used.
*/
String value() default "";
/**
* Provide a longer description of the class.
*/
String description() default "";
/**
* Provide a superclass for the model to allow describing inheritance.
*/
Class<?> parent() default Void.class;
/**
* Supports model inheritance and polymorphism.
* <p>
* This is the name of the field used as a discriminator. Based on this field,
* it would be possible to assert which sub type needs to be used.
*/
String discriminator() default "";
/**
* An array of the sub types inheriting from this model.
*/
Class<?>[] subTypes() default {
};
/**
* Specifies a reference to the corresponding type definition, overrides any other metadata specified
*/
String reference() default "";
}
@ApiModel
中value
默认为字段名,上面案例的写法是指定了两个相同的value
值,在调试源码发现@ApiModel
中value存在相同值时,全局中只会保留第一个,后面的只会按照第一个类的属性进行解析展示。
处理方案
明白原因之后就好处理了,可以将其中一个value值进行修改即可。当然也可以使用@ApiModel
中description属性用于描述类信息(可以相同),value保持缺省状态即默认使用字段名来保持唯一。按照第一种修改之后的请求参数与接口文档截图如下:
至此问题解决,时间不早了,先到这吧.如果感觉有帮助欢迎点赞留言或是收藏表达一下支持!