我为我的 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/