# 分享一个小工具类（思路）解决swagger文档的小痛点

* 抖音：<https://v.douyin.com/iUBMrkUq/>
* B站：<https://www.bilibili.com/video/BV1aU411U7BT/>

正常情况

Java在这种注解里面只能使用常量值

但是呢有些时候

我们确实希望在里面写一些动态的值

比如说前两天

在给一个旧项目

维护他的swagger文档时候

我就希望这个allowableValues

是从这个枚举里边

自动读取出来

这些值放进去

但是呢他也没办法写这种动态值

所以今天给大家分享一个思路

和写好的小的工具类啊

就是我们通过注解

我们再写一个注解

在这里啊

写好了叫Enum2AllowableValues

然后呢我们在这里边指定value

然后指定这个枚举

它就行

type class

然后呢

我们还可以指定它用哪个属性

作为它的指像

这个是value

所以呢我们这块儿写method=getValue

好我们再来试一下

把这个删掉

OK我们再试一下

大家看这个值就已经有了

那这块还能这样写

比如说这里边还有一个desc属性

或者往上getDesc

那它生成的就是这样的

我们给大家看一下

c:Add，U:Upadte

就是这样的

那这个思路其实很容易

就是我们把这个注解写好之后呢

写了一个command line runner

就是在项目启动的时候

我们通过大家把这个包名

改成自己的项目的包名啊

然后我们主要是通过反射

取到这个注解的

就是这个schema的memberValues

然后去设置里边儿的allowableValues

然后这个代码呢

我还是会放到这个地方

大家如果需要可以作为参考


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sunzsh.gitbook.io/xiaoshan.bug/fen-xiang-yi-ge-xiao-gong-ju-lei-si-lu-jie-jue-swagger-wen-dang-de-xiao-tong-dian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
