Swagger可视化API,不仅能查看API,还能测试
1.引入相关JAR
<dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> 2.在启动类中加入Swagger相关代码(用的springboot): @EnableSwagger public class AppServiceApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(AppServiceApplication.class); Set<Object> sourcesSet = new HashSet<Object>(); sourcesSet.add("classpath:applicationContext.xml"); application.setSources(sourcesSet); application.run(args); } private SpringSwaggerConfig springSwaggerConfig; /** * Required to autowire SpringSwaggerConfig */ @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; } /** * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc * framework - allowing for multiple swagger groups i.e. same code base * multiple swagger resource listings. */ @Bean public SwaggerSpringMvcPlugin customImplementation() { return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?"); } private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo( "App Service API", "", "", "huqc@gcks.cn", "", ""); return apiInfo; }修改index.html
将index.html中http://petstore.swagger.wordnik.com/v2/swagger.json修改为http://localhost:8080/{projectname}/api-docs
到此为止,所有配置完成,启动你的项目,访问http://localhost:8086/swagger/index.html即可看到如下所示页面:
但是swagger不建议像上面和代码混合在一块,建议swagger用yaml文件单独配置部署生成HTML文件