|
| 1 | +package com.baeldung.camel; |
| 2 | + |
| 3 | +import javax.ws.rs.core.MediaType; |
| 4 | + |
| 5 | +import org.apache.camel.CamelContext; |
| 6 | +import org.apache.camel.Exchange; |
| 7 | +import org.apache.camel.Processor; |
| 8 | +import org.apache.camel.builder.RouteBuilder; |
| 9 | +import org.apache.camel.component.servlet.CamelHttpTransportServlet; |
| 10 | +import org.apache.camel.impl.DefaultCamelContext; |
| 11 | +import org.apache.camel.model.rest.RestBindingMode; |
| 12 | +import org.springframework.beans.factory.annotation.Value; |
| 13 | +import org.springframework.boot.SpringApplication; |
| 14 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 15 | +import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| 16 | +import org.springframework.boot.web.support.SpringBootServletInitializer; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.context.annotation.ComponentScan; |
| 19 | +import org.springframework.stereotype.Component; |
| 20 | + |
| 21 | +@SpringBootApplication |
| 22 | +@ComponentScan(basePackages="com.baeldung.camel") |
| 23 | +public class Application extends SpringBootServletInitializer { |
| 24 | + |
| 25 | + @Value("${server.port}") |
| 26 | + String serverPort; |
| 27 | + |
| 28 | + @Value("${baeldung.api.path}") |
| 29 | + String contextPath; |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + SpringApplication.run(Application.class, args); |
| 33 | + } |
| 34 | + |
| 35 | + @Bean |
| 36 | + ServletRegistrationBean servletRegistrationBean() { |
| 37 | + ServletRegistrationBean servlet = new ServletRegistrationBean(new CamelHttpTransportServlet(), contextPath+"/*"); |
| 38 | + servlet.setName("CamelServlet"); |
| 39 | + return servlet; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + @Component |
| 44 | + class RestApi extends RouteBuilder { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void configure() { |
| 48 | + |
| 49 | + CamelContext context = new DefaultCamelContext(); |
| 50 | + |
| 51 | + |
| 52 | + // http://localhost:8080/camel/api-doc |
| 53 | + restConfiguration().contextPath(contextPath) // |
| 54 | + .port(serverPort) |
| 55 | + .enableCORS(true) |
| 56 | + .apiContextPath("/api-doc") |
| 57 | + .apiProperty("api.title", "Test REST API") |
| 58 | + .apiProperty("api.version", "v1") |
| 59 | + .apiProperty("cors", "true") // cross-site |
| 60 | + .apiContextRouteId("doc-api") |
| 61 | + .component("servlet") |
| 62 | + .bindingMode(RestBindingMode.json) |
| 63 | + .dataFormatProperty("prettyPrint", "true"); |
| 64 | +/** |
| 65 | +The Rest DSL supports automatic binding json/xml contents to/from POJOs using Camels Data Format. |
| 66 | +By default the binding mode is off, meaning there is no automatic binding happening for incoming and outgoing messages. |
| 67 | +You may want to use binding if you develop POJOs that maps to your REST services request and response types. |
| 68 | +This allows you, as a developer, to work with the POJOs in Java code. |
| 69 | +*/ |
| 70 | + |
| 71 | + rest("/api/").description("Teste REST Service") |
| 72 | + .id("api-route") |
| 73 | + .post("/bean") |
| 74 | + .produces(MediaType.APPLICATION_JSON) |
| 75 | + .consumes(MediaType.APPLICATION_JSON) |
| 76 | +// .get("/hello/{place}") |
| 77 | + .bindingMode(RestBindingMode.auto) |
| 78 | + .type(MyBean.class) |
| 79 | + .enableCORS(true) |
| 80 | +// .outType(OutBean.class) |
| 81 | + |
| 82 | + .to("direct:remoteService"); |
| 83 | + |
| 84 | + |
| 85 | + from("direct:remoteService") |
| 86 | + .routeId("direct-route") |
| 87 | + .tracing() |
| 88 | + .log(">>> ${body.id}") |
| 89 | + .log(">>> ${body.name}") |
| 90 | +// .transform().simple("blue ${in.body.name}") |
| 91 | + .process(new Processor() { |
| 92 | + @Override |
| 93 | + public void process(Exchange exchange) throws Exception { |
| 94 | + MyBean bodyIn = (MyBean) exchange.getIn().getBody(); |
| 95 | + |
| 96 | + ExampleServices.example(bodyIn); |
| 97 | + |
| 98 | + exchange.getIn().setBody(bodyIn); |
| 99 | + } |
| 100 | + }) |
| 101 | + .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201)); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments