我按照Swagger-springmvc README中的描述为我的Spring MVC项目设置了Swagger。我使用Spring Java Configuration而不是基于XML的Spring配置。作为构建系统,我使用Gradle。
当我向GET
发出api-docs
请求时,我得到了以下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;
}
}
你能帮我解决这个问题吗?
答案 0 :(得分:1)
将包含模式更改为
.includePatterns(".*url.*");
应该解决你的问题。问题是,您没有任何带有 pet 字样的请求映射。