BAEL-798 - Apache Camel with Spring Boot (#1761)
* initial import * simplified to rest only * simplifications * update * Create ExampleServices.java * Update Application.java * simple readme * strip of dependency management * setting Camel versions apart where affected * Update README.md * update comments * Update pom.xml * delete of fabri8 deployment.yml
This commit is contained in:
parent
1a73061761
commit
70ae331cd7
|
@ -0,0 +1,15 @@
|
|||
Example for the Article on Camel API with SpringBoot
|
||||
|
||||
to start up, run:
|
||||
mvn spring-boot:run
|
||||
|
||||
them, do a POST http request to:
|
||||
http://localhost:8080/camel/api/bean
|
||||
|
||||
with the HEADER: Content-Type: application/json,
|
||||
|
||||
and a BODY Payload like {"id": 1,"name": "World"}
|
||||
|
||||
and we will get a return code of 201 and the response: Hello, World - if the transform() method from Application class is uncommented and the process() method is commented
|
||||
|
||||
or return code of 201 and the response: {"id": 10,"name": "Hello, World"} - if the transform() method from Application class is commented and the process() method is uncommented
|
|
@ -0,0 +1,80 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>spring-boot-camel</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<name>Spring-Boot - Camel API</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<camel.version>2.19.1</camel.version>
|
||||
<spring-boot-starter.version>1.5.4.RELEASE</spring-boot-starter.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-servlet-starter</artifactId>
|
||||
<version>${camel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-jackson-starter</artifactId>
|
||||
<version>${camel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-swagger-java-starter</artifactId>
|
||||
<version>${camel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.camel</groupId>
|
||||
<artifactId>camel-spring-boot-starter</artifactId>
|
||||
<version>${camel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>spring-boot:run</defaultGoal>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,104 @@
|
|||
package com.baeldung.camel;
|
||||
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.Processor;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
|
||||
import org.apache.camel.impl.DefaultCamelContext;
|
||||
import org.apache.camel.model.rest.RestBindingMode;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages="com.baeldung.camel")
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
||||
@Value("${server.port}")
|
||||
String serverPort;
|
||||
|
||||
@Value("${baeldung.api.path}")
|
||||
String contextPath;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
ServletRegistrationBean servletRegistrationBean() {
|
||||
ServletRegistrationBean servlet = new ServletRegistrationBean(new CamelHttpTransportServlet(), contextPath+"/*");
|
||||
servlet.setName("CamelServlet");
|
||||
return servlet;
|
||||
}
|
||||
|
||||
|
||||
@Component
|
||||
class RestApi extends RouteBuilder {
|
||||
|
||||
@Override
|
||||
public void configure() {
|
||||
|
||||
CamelContext context = new DefaultCamelContext();
|
||||
|
||||
|
||||
// http://localhost:8080/camel/api-doc
|
||||
restConfiguration().contextPath(contextPath) //
|
||||
.port(serverPort)
|
||||
.enableCORS(true)
|
||||
.apiContextPath("/api-doc")
|
||||
.apiProperty("api.title", "Test REST API")
|
||||
.apiProperty("api.version", "v1")
|
||||
.apiProperty("cors", "true") // cross-site
|
||||
.apiContextRouteId("doc-api")
|
||||
.component("servlet")
|
||||
.bindingMode(RestBindingMode.json)
|
||||
.dataFormatProperty("prettyPrint", "true");
|
||||
/**
|
||||
The Rest DSL supports automatic binding json/xml contents to/from POJOs using Camels Data Format.
|
||||
By default the binding mode is off, meaning there is no automatic binding happening for incoming and outgoing messages.
|
||||
You may want to use binding if you develop POJOs that maps to your REST services request and response types.
|
||||
This allows you, as a developer, to work with the POJOs in Java code.
|
||||
*/
|
||||
|
||||
rest("/api/").description("Teste REST Service")
|
||||
.id("api-route")
|
||||
.post("/bean")
|
||||
.produces(MediaType.APPLICATION_JSON)
|
||||
.consumes(MediaType.APPLICATION_JSON)
|
||||
// .get("/hello/{place}")
|
||||
.bindingMode(RestBindingMode.auto)
|
||||
.type(MyBean.class)
|
||||
.enableCORS(true)
|
||||
// .outType(OutBean.class)
|
||||
|
||||
.to("direct:remoteService");
|
||||
|
||||
|
||||
from("direct:remoteService")
|
||||
.routeId("direct-route")
|
||||
.tracing()
|
||||
.log(">>> ${body.id}")
|
||||
.log(">>> ${body.name}")
|
||||
// .transform().simple("blue ${in.body.name}")
|
||||
.process(new Processor() {
|
||||
@Override
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
MyBean bodyIn = (MyBean) exchange.getIn().getBody();
|
||||
|
||||
ExampleServices.example(bodyIn);
|
||||
|
||||
exchange.getIn().setBody(bodyIn);
|
||||
}
|
||||
})
|
||||
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.camel;
|
||||
|
||||
/**
|
||||
* a Mock class to show how some other layer
|
||||
* (a persistence layer, for instance)
|
||||
* could be used insida a Camel
|
||||
*
|
||||
*/
|
||||
public class ExampleServices {
|
||||
|
||||
public static void example(MyBean bodyIn) {
|
||||
bodyIn.setName( "Hello, " + bodyIn.getName() );
|
||||
bodyIn.setId(bodyIn.getId()*10);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.camel;
|
||||
|
||||
public class MyBean {
|
||||
private Integer id;
|
||||
private String name;
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
logging.config=classpath:logback.xml
|
||||
|
||||
# the options from org.apache.camel.spring.boot.CamelConfigurationProperties can be configured here
|
||||
camel.springboot.name=MyCamel
|
||||
|
||||
# lets listen on all ports to ensure we can be invoked from the pod IP
|
||||
server.address=0.0.0.0
|
||||
management.address=0.0.0.0
|
||||
|
||||
# lets use a different management port in case you need to listen to HTTP requests on 8080
|
||||
management.port=8081
|
||||
|
||||
# disable all management enpoints except health
|
||||
endpoints.enabled = true
|
||||
endpoints.health.enabled = true
|
|
@ -0,0 +1,27 @@
|
|||
server:
|
||||
port: 8080
|
||||
|
||||
# for example purposes of Camel version 2.18 and below
|
||||
baeldung:
|
||||
api:
|
||||
path: '/camel'
|
||||
|
||||
camel:
|
||||
springboot:
|
||||
# The Camel context name
|
||||
name: ServicesRest
|
||||
|
||||
# Binding health checks to a different port
|
||||
management:
|
||||
port: 8081
|
||||
|
||||
# disable all management enpoints except health
|
||||
endpoints:
|
||||
enabled: false
|
||||
health:
|
||||
enabled: true
|
||||
|
||||
# The application configuration properties
|
||||
quickstart:
|
||||
generateOrderPeriod: 10s
|
||||
processOrderPeriod: 30s
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE xml>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoders are assigned the type
|
||||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
Loading…
Reference in New Issue