【发布时间】:2017-03-04 04:10:23
【问题描述】:
我只是想将 Swagger 集成到我用 Gradle 构建的 Spring Boot (JAX-RS) 项目中。我能够生成与以下相同的泊坞窗(Swagger UI):
我已经使用默认设置配置了我的招摇:
package com.abc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableAutoConfiguration
@SpringBootApplication
@EnableMongoRepositories
@Slf4j
@Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class,springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
@EnableSwagger2
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
public static void run(String[] args) throws Exception{
log.info("Started application on: 8080");
}
}
正如我们在 GET Events API 的图像中看到的,泊坞窗显示 /eventses .. 所以它从哪里添加 es 到 /events API,写成:
@GET
public HashMap<String, Object> getEventList(@DefaultValue("1") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("rpp") int rpp, @QueryParam("events") String eventIds) {
HashMap<String, Object> eventsResultMap= new HashMap<String, Object>();
List<Events> events = null;
if (eventIds != null && eventIds.length() > 0) {
List<String> eventsIdList = Arrays.asList(eventIds.split(","));
log.info("" + eventsIdList);
events = eventService.getEvents(eventsIdList);
} else {
events = eventService.getEvents(page - 1, rpp);
}
eventsResultMap.put("EVENTS", events);
HashMap<String, Object> recordsMetaMap = new HashMap<String, Object>();
recordsMetaMap.put("total", eventService.totalCount());
recordsMetaMap.put("page", page);
recordsMetaMap.put("rpp", rpp);
eventsResultMap.put("_metadata", recordsMetaMap);
log.info("The events you have queried for are:" + eventsResultMap);
return eventsResultMap;
}
请指导我哪里做错了。需要做哪些自定义配置。
我从 spring 官方文档中获取了Reference。
【问题讨论】:
-
我认为它没有正确读取该方法。例如它写为
@GET,但在图片中显示为POST和其他类型。只是一个想法....我认为es是由于HashMap的返回类型。你试过使用@ApiOperation注解吗?
标签: spring spring-boot jax-rs swagger-ui swagger-2.0