简体   繁体   中英

Swagger APIs array is empty for SpringMVC app

I setup Swagger for my Spring MVC project as described in Swagger-springmvc README . I use Spring Java Configuration not XML-based Spring configuration. As build system I use Gradle.

When I perform GET request to api-docs I get following 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"}

My problem is that the apis array is empty.

I have controller class with following structure

@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);
}

}

Also, I tried to add @RequestMapping("/path") annotation just before class declaration but it did not help me.

My configuration class

@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;
    }
} 

Could you please help me with the issue?

Changing your include pattern to

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

Should fix your problem. The problem is that the you don't have any request mappings with the word pet in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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