繁体   English   中英

Swagger API数组对于SpringMVC应用为空

[英]Swagger APIs array is empty for SpringMVC app

我为Swagger-springmvc README中所述的Spring MVC项目设置了Swagger。 我使用Spring Java Configuration而不是基于XML的Spring配置。 作为构建系统,我使用Gradle。

当我执行对api-docs GET请求时,得到以下JSON

 {"apiVersion":"1.0","apis":[],"authorizations":{},"info":{"contact":"My Apps API Contact Email","description":"My Apps API Description","license":"My Apps API Licence Type","licenseUrl":"My Apps API License URL","termsOfServiceUrl":"My Apps API terms of service","title":"My Apps API Title"},"swaggerVersion":"1.2"}

我的问题是apis数组为空。

我有以下结构的控制器类

@Api(value= "path", description = "Description")
@Controller
public class MyController {

public static final String URL_1 = "/url1";
public static final String URL_2 = "/url2";

@Autowired
private SomeService service;

@Autowired
private SomeRepository repository;

@Autowired
private SomeConverter converter;

@RequestMapping(method = RequestMethod.POST, value = URL1)
public void nextTask(HttpServletResponse response) throws IOException {
    PrintWriter writer = response.getWriter();
    Task task = getNextTask();
    String taskAsText = converter.toTextHandledWithComputeNode(task);
    writer.write(taskAsText);
}

@RequestMapping(method = RequestMethod.POST, value = URL_2)
@ResponseStatus(value = HttpStatus.OK)
public void taskResult(@PathVariable("id") String id) {
    service.mark(id);
}

}

另外,我试图在类声明之前添加@RequestMapping("/path")批注,但这对我没有帮助。

我的配置类

@Configuration
@EnableWebMvc
@EnableSwagger
@ComponentScan(basePackages = {"com.my.controller"})
public class MVCConfig {
    private SpringSwaggerConfig springSwaggerConfig;

    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
    this.springSwaggerConfig = springSwaggerConfig;
}

     @Bean //Don't forget the @Bean annotation
     public SwaggerSpringMvcPlugin customImplementation(){
         return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
            .apiInfo(apiInfo())
            .includePatterns(".*pet.*");
}

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
            "My Apps API Title",
            "My Apps API Description",
            "My Apps API terms of service",
            "My Apps API Contact Email",
            "My Apps API Licence Type",
            "My Apps API License URL"
        );
        return apiInfo;
    }
} 

您能帮我解决这个问题吗?

将您的包含模式更改为

            .includePatterns(".*url.*");

应该解决您的问题。 问题是您没有任何带有pet字样的请求映射。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM