在有些情况下,有一些下拉选择器的数据项,我们采取了枚举的方式返回前端一个列表,但是里面的东西多,前端不想写死,需要提供接口返回,如下图类似这种
第一步:先创建一个枚举类
@AllArgsConstructor
public enum GoodsTypeEnum {
SCENIC_TICKET(1, "景点门票(单票)"),
THIRD_JOINT_TICKET (2,"第三方联票/券码类商品");
public final Integer code;
public final String ticketTypName;
public static String getGoodsName(Integer code) {
if (Objects.nonNull(code)) {
for (GoodsTypeEnum value : values()) {
if (value.code.equals(code)) {
return value.ticketTypName;
}
}
}
return null;
}
第二步:因为有些格式不支持,我使用的是创建对象方式
@Data
public class GoodsTypeBean {
int ticketType;
String ticketTypName;
}
第三步:定义Service接口及实现
Proto ticketBusinessType(QueryProductTypeReq req);
//实现接口
@Override
public Proto ticketBusinessType(QueryProductTypeReq req) {
List list = new ArrayList<>();
for (GoodsTypeEnum goodsTypeEnum : EnumSet.allOf(GoodsTypeEnum.class)) {
GoodsTypeBean goodsTypeBean = new GoodsTypeBean();
goodsTypeBean.setTicketType(goodsTypeEnum.code);
goodsTypeBean.setTicketTypName(goodsTypeEnum.ticketTypName);
list.add(goodsTypeBean);
}
return Protos.ok(Collections.singletonMap("data", list));
}
第四步:编写controller
@PostMapping("/scenic/goods/ticketBusinessType")
public Proto ticketBusinessType(@RequestBody QueryProductTypeReq req) {
return productModelService.ticketBusinessType(req);
}
第五步:postman接口测试
测试OK,可以以列表方式返回
本人写博文完全是为了自己去巩固一些知识点,做笔记,但同时也希望可以帮助到大家,如果有什么不好的地方,也希望大家见谅!希望大家一起进步!!



