swagger - Springfox/swagger 忽略 @XmlElements
问题描述
我想为以下控制器添加 swagger API
@RestController
public class MyController {
@PostMapping("/box")
public String post(@RequestBody MyBox myBox) {
return "Success";
}
}
我实际上正在关注 myBox 示例值:
{
"BoxName": "string",
"items": [
{}
]
}
但我需要这样:
{
"BoxName" : "string",
"items" : [ {
"MyItem1" : {
"name" : "string"
}
}, {
"MyItem2" : {
"description" : "string"
}
} ]
}
MyBox类:
@XmlType(name = "MyBox")
public class MyBox {
@XmlElement(name = "BoxName")
protected String boxName;
@XmlElements({
@XmlElement(name = "MyItem1", type = MyItem1.class),
@XmlElement(name = "MyItem2", type = MyItem2.class),
})
protected List<Object> items;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyItem1")
public class MyItem1 {
@XmlElement(name = "name")
private String name;
}
@XmlType(name = "MyItem2")
public class MyItem2 {
@XmlElement(name = "name")
private String description;
}
我添加了以下Swagger
和ObjectMapper
配置:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.alternateTypeRules()
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.rest"))
.paths(PathSelectors.any())
.build();
}
}
@Configuration
public class ApplicationConfiguration {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.modules(new JaxbAnnotationModule().setPriority(JaxbAnnotationModule.Priority.PRIMARY))
.build()
.enable(SerializationFeature.INDENT_OUTPUT);
}
}
我无法更改(添加模型映射)到MyBox
和MyItem1
类MyItem2
,它们是自动生成的。
我添加了演示项目来重现该问题: https ://github.com/ivan333m/springfox-jaxb
有人可以指出我正确的方向吗?
解决方案
推荐阅读
- java - 如何在关闭 Shell 时捕获 [x]
- c# - 使用 Swagger 在 ASP.NET Core 中实现 OAuth
- python - 如何使用 tsne 可视化高维数据?
- c# - PDF 未正确解码...我从 API Call OK 打开 PDF,但保存后无法打开(VS 2015 - C#.NET Framework 4.8)
- python - 如何创建一个带有不可变变量的python dict,这些变量正在发生变化?
- angular - Angular 11 模块的理想尺寸是多少?
- algorithm - 最小化排列总和的贪心算法
- angular - 如何使用 RXJS 在 Angular 中等待多个 HTTP 调用完成?
- javascript - react-router v6:获取当前路由的路径模式
- c++ - 将 std::string 中的每个字符存储到 std::string