全新的骆驼+ SpringBoot +摇摆。使用Camel 3.8.0和SpringBoot 2.4.2创建了一些REST API(在默认的TomCat of SpringBoot上使用Camel的Servlet )。
这是pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/>
</parent>
<groupId>com.crsardar.java.apache.camel</groupId>
<artifactId>hands-on-camel-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<camelVersion>3.8.0</camelVersion>
</properties>
<dependencies>
<!-- SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>${camelVersion}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camelVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camelVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camelVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-swagger-starter</artifactId>
<version>${camelVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-swagger-java</artifactId>
<version>${camelVersion}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</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>
<!-- Others -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>配置和休息端点-
package com.crsardar.java.apache.camel;
import com.crsardar.java.dao.Order;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
@Component
public class CamelController extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration()
.component("servlet")
.port(8080)
.host("127.0.0.1")
.apiContextPath("api-docs")
.apiContextIdPattern("#name#")
.apiProperty("api.title", "Test REST API")
.apiProperty("api.version", "v1")
.apiProperty("cors", "true")
.bindingMode(RestBindingMode.json);
rest().post("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.type(Order.class)
.outType(Order.class)
.to("bean:orderService?method=addOrder(${body})");
rest().get("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.to("bean:orderService?method=getOrders()");
}
}我试图记录它,并给出一个选项来测试它使用Swagger。
如果我运行应用程序并点击http://127.0.0.1:8080/api-docs,我将获得Swagger的API文档。
但是,我不能使用Swagger来尝试它,我如何使Swagger在上面工作呢?
我不知道--是否Swagger正在使用这个应用程序?如果有效,Swagger的URL是什么?
完整的代码在这里,https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot
发布于 2021-10-06 14:31:23
要为默认的Camel配置启用Swagger,SpringBoot 2.2.7、Camel 3.11.2和Swagger2.9.2:
添加到您的pom.xml
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>添加配置文件以启用swagger-ui:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}添加application.properties
springfox.documentation.swagger.v2.path=/camel/api-doc路由器配置:
@Override
public void configure() throws Exception {
String contextPath = env.getProperty("camel.servlet.mapping.context-path", "/camel");
//Common Rest config
restConfiguration()
.component("servlet")
.bindingMode(RestBindingMode.json)
.contextPath(contextPath)
.enableCORS(true)
//Enable swagger endpoint.
.dataFormatProperty("prettyPrint", "true")
// add swagger api-doc out of the box
.apiContextPath("/api-doc")
.apiProperty("api.title", "API Title")
.apiProperty("api.version", "v1")
// and enable CORS
.apiProperty("cors", "true");
}Swagger-UI将在地址http://localhost:8080/swagger-ui.html上处于活动状态。
https://stackoverflow.com/questions/66608670
复制相似问题