有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我为我的SpringMVC项目设置了Swagger,如Swagger-springmvc README中所述。我使用SpringJava配置,而不是基于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;
    }
} 

你能帮我解决这个问题吗


共 (1) 个答案

  1. # 1 楼答案

    将包含模式更改为

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

    应该能解决你的问题。问题是,您没有任何包含单词pet的请求映射