java - SpringMVC 应用程序的 Swagger API 数组为空

标签 java spring spring-mvc gradle swagger

我为我的 Spring MVC 项目设置了 Swagger,如 Swagger-springmvc README 中所述。 。我使用 Spring Java 配置而不是基于 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 数组为空。

我有具有以下结构的 Controller 类

@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 一词的请求映射。

关于java - SpringMVC 应用程序的 Swagger API 数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28989549/

相关文章:

java - 从不使用 spring ldap 设置 tls_reqcert

Spring MVC - Bean 不适用于 @Autowired

spring-mvc - Spring - 获取正在使用的域名

java - 垂直表格标题单元格渲染器

java - 测试驱动开发。如何在创建方法之前为此转换编写单元测试?

java - 匹配器断言两个对象

java - Jquery Ajax表单提交成功前如何刷新页面?

Java正则表达式

java - 在构造函数中引用现有文件时 Windows Java 文件锁定?

java - 使用 Spring 和 Jackson 将 Json 数组解析为 Pojo?