org.springframework.cloud
spring-cloud-dependencies
Dalston.SR1
pom
import
- 第二步:编写应用主类:
@EnableSwagger2Doc
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
@RestController
class AaaController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping(“/service-a”)
public String dc() {
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}
}
}
其中,@EnableSwagger2Doc
注解是我们自制Swagger Starter中提供的自定义注解,通过该注解会初始化默认的Swagger文档设置。下面还创建了一个通过Spring MVC编写的HTTP接口,用来后续在文档中查看使用。
- 第三步:设置配置文件内容:
spring.application.name=swagger-service-a
server.port=10010
eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
swagger.base-package=com.didispace
其中,eureka服务端的配置采用了本站的公益eureka,大家可以通过http://eureka.didispace.com/查看详细以及使用方法。另外,swagger.base-package
参数制定了要生成文档的package,只有com.didispace
包下的Controller才会被生成文档。
注意:上面构建了swagger-service-a服务,swagger-service-b服务可以如法炮制,不再赘述。<