Camel - Rest Component 集成 Swagger UI
1. Spring boot 集成 Swagger UI
1.1 pom配置
本示例通过webjar的方式集成swagger UI。
swagger-ui版本为2.2.10.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.40</version>
</dependency>
配置spring boot 默认启动 tomcat 服务器,启动端口配置为 8088
server.port=8088
1.2 访问swagger ui
http://localhost:8088/webjars/swagger-ui/index.html#/
2. Camel配置并启用Rest服务
2.1 pom配置
<!-- camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jetty-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-swagger-java</artifactId>
<version>3.6.0</version>
</dependency>
2.2 配置并启动jetty服务
配置jetty服务,端口为8080,同时支持 swagger api 服务。
代码参考 https://github.com/ttyy1112/Camel-In-Action-3.3.0/tree/master/spring-app/rest-camel-springboot-test
public class OrderRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration()
.component("jetty")
.port(8080)
//Enable swagger endpoint.
.apiContextPath("/swagger") //swagger endpoint path
.apiContextRouteId("swagger") //id of route providing the swagger endpoint
//Swagger properties
.contextPath("/api") //base.path swagger property; use the mapping path set for CamelServlet
.apiProperty("api.title", "Swagger UI with Apache Camel-Jetty")
.apiProperty("api.version", "1.0")
.apiProperty("api.contact.name", "ATang")
.apiProperty("api.contact.email", "ATang@atang.com")
.apiProperty("api.contact.url", "https://atang.com/")
.apiProperty("host", "") //by default 0.0.0.0
.apiProperty("port", "8080")
.apiProperty("schemes", "")
.apiProperty("cors", "true");
// rest services under the orders context-path
rest("/orders")
// need to specify the POJO types the binding is using (otherwise json binding defaults to Map based)
.get("{id}").outType(Order.class)
.to("bean:orderService?method=getOrder(${header.id})")
// need to specify the POJO types the binding is using (otherwise json binding defaults to Map based)
.post().type(Order.class)
.to("bean:orderService?method=createOrder")
// need to specify the POJO types the binding is using (otherwise json binding defaults to Map based)
.put().type(Order.class)
.to("bean:orderService?method=updateOrder")
.delete("{id}")
.to("bean:orderService?method=cancelOrder(${header.id})");
}
}
2.3 启动并获取swagger api信息
通过访问 http://localhost:8080/api/swagger ,可以获取swagger api信息。
3. 测试
参考
Integrate Swagger with Apache Camel-Servlet.
Spring Boot, Apache Camel, and Swagger UI
SWAGGER JAVA
集成Open API
Swagger技术(接口文档实时动态生成工具)