@组件 公共类 BookRoute 扩展 RouteBuilder {
private final Environment env;
public BookRoute(Environment env) {
this.env = env;
}
public void configure() throws Exception {
restConfiguration()
.contextPath(env.getProperty("camel.component.servlet.mapping.contextPath", "/rest/*"))
.apiContextPath("/api-doc")
.apiProperty("api.title", "Spring Boot Camel Postgres Rest API.")
.apiProperty("api.version", "1.0")
.apiProperty("cors", "true")
.apiContextPath("doc-api")
.port(env.getProperty("server.port", "8080"))
.bindingMode(RestBindingMode.json);
rest("/book")
.consumes(MediaType.APPLICATION_JSON_VALUE)
.produces(MediaType.APPLICATION_JSON_VALUE)
.get("/{name}").route()
.to("{{route.findBookByName}}")
.endRest()
.get("/").route()
.to("{{route.findAllBooks}}")
.endRest()
.post("/").route()
.marshal().json()
.unmarshal(getJacksonDataFormat(Book.class))
.to("{{route.saveBook}}")
.endRest()
.delete("/{bookId}").route()
.to("{{route.removeBook}}")
.end();
from("{{route.findBookByName}}")
.log("Received header : ${header.name}")
.bean(BookService.class, "findBookByName(${header.name})");
from("{{route.findAllBooks}}")
.bean(BookService.class, "findAllBooks");
from("{{route.saveBook}}")
.log("Received Body ${body}")
.bean(BookService.class, "addBook(${body})");
from("{{route.removeBook}}")
.log("Received header : ${header.bookId}")
.bean(BookService.class, "removeBook(${header.bookId})");
}
private JacksonDataFormat getJacksonDataFormat(Class<?> unmarshalType) {
JacksonDataFormat format = new JacksonDataFormat();
format.setUnmarshalType(unmarshalType);
return format;
}
}
上面的代码可以正常使用“
请参阅 Camel 升级指南,了解 3.16 及之后的 rest-dsl 的变化 https://camel.apache.org/manual/camel-3x-upgrade-guide-3_16.html#_rest_dsl