gpt4 book ai didi

java - 如何将文件内容传递给 swagger @ExampleProperty 注释值?

转载 作者:行者123 更新时间:2023-12-02 09:57:45 25 4
gpt4 key购买 nike

我正在使用 swagger 3.0.0-Snapshot 为我的 Spring Boot 应用程序创建文档。我的 Maven 依赖项是

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webmvc</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>

我的 swagger 配置类尽可能简单:

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.mycompany.cs"))
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.useDefaultResponseMessages(false);
}

我的 Controller 方法有以下注释:

@ApiOperation(value = "Hello world", httpMethod = "POST")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK",
examples = @Example(value = @ExampleProperty(mediaType = "application/json",
value = exampleValue)))
})

它正在工作,并在 Swagger UI“示例值”字段值中显示,该字段值具有私有(private)静态字符串常量字符串 exampleValue。

问题是如何将资源文件夹中的json文件的内容传递给@ExampleProperty值?

我尝试读取静态 block 中的文件内容并将其传递给它来初始化最终的字符串,但编译器说“属性值必须是常量”。

json 文件的内容必须显示在 Swagger UI 的示例字段中。

最佳答案

好消息是 Swagger 正在使用 Spring,并且可以利用 DI 的强大功能。

例如,您想要向 ServiceModelToSwagger2MapperImpl 添加新功能。创建您自己的组件来扩展它并将其标记为主要组件。 Spring 将 Autowiring ServiceModelToSwagger2Mapper 抽象类的实现。

@Component
@Primary
@Slf4j
public class ServiceModelToSwagger2MapperExtensionImpl extends ServiceModelToSwagger2MapperImpl {

例如,您希望它读取文件的内容并将其放入示例字段:

@Override
protected Map<String, Response> mapResponseMessages(Set<ResponseMessage> from) {
Map<String, Response> responses = super.mapResponseMessages(from);
responses.forEach((key, response)-> {
Map<String, Object> examples = response.getExamples();
examples.entrySet().forEach(example -> {
Object exampleObject = example.getValue();
if (exampleObject instanceof String) {
String exampleValue = (String) exampleObject;
if (exampleValue.startsWith("file:")) {
String fileContent = readFileContent(exampleValue);
example.setValue(fileContent);
}
}});
});

return responses;
}

private String readFileContent(String example) {
String fileContent = "";
try {
String fileName = example.replace("file:", "");
File resource = new ClassPathResource(fileName).getFile();
if(resource.exists()) {
fileContent
= new String(Files.readAllBytes(resource.toPath()));
}
} catch (
IOException e) {
log.error("Cannot read swagger documentation from file {}", example);
}
return fileContent;
}

这是 Controller 中的使用示例:

@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK",
examples = @Example(value = @ExampleProperty(mediaType = "application/vnd.siren+json",
value = "file:/data/controller-responses/reponse.json")))
})

关于java - 如何将文件内容传递给 swagger @ExampleProperty 注释值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55869915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com