这是一个 Spring 引导应用程序,正如我在主题“appConfig”中提到的,当我添加 Swagger 时,响应中缺少根元素。在此论坛上的任何帮助将不胜感激。
这是我的响应对象类:
@JsonRootName(value = "appConfig")
public class AppConfig {
// Using lombok for getter setters
@Getter
@Setter
@JsonProperty("toggles")
private List<Toggle> toggles;
@Getter
@Setter
@JsonProperty("resources")
private List<Resource> resources;
这是我的restController
@RequestMapping(value = "/appConfig", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE })
@ResponseStatus(HttpStatus.OK)
public AppConfig appConfig() {
final AppConfig appConfig =delegate.getAppConfig();
return appConfig;
}
这就是我在响应中得到的
//MISSING appConfig root element !!!!
{
"resources": [
{
"lastUpdateTimeStamp": "string",
"resourceName": "string"
}
],
"toggles": [
{
"name": "string",
"state": true
}
]
}
这是我对 Swagger 的 POM 依赖:
<!-- Swagger dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
这是我的 Swagger 配置类:
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
private ApiInfo apiInfo() {
return new ApiInfo(
"Blah",
"Blah",
"Blah",
"Terms of service",
new Contact("Blah Administrator", "URL", "Email"),
"License of API", "API license URL");
}
最佳答案
变通变丑!是定义一个虚拟响应对象并将您的响应包装在其中。所以 Swagger 剥离了它并给出了预期的答案。仍在寻找答案!
关于java - 使用 Swagger 注释时响应中缺少 JSON 父/根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49672854/