From 5ba204bd59f66639d092cd81a8c2ad98dad0df0e Mon Sep 17 00:00:00 2001 From: "OCTOPUSCARDS\\yanng" Date: Sun, 5 Jan 2025 17:22:50 +0800 Subject: [PATCH] Update Camel from v4.7.0 to v4.8.2 --- conf/springboot.yml | 5 +- pom.xml | 117 ++++++++---------- readme.adoc | 91 ++++++++++++++ src/main/java/com/example/camel/Boot.java | 2 - .../java/com/example/camel/CamelRouter.java | 4 +- src/main/resources/camel-route.xml | 90 -------------- 6 files changed, 153 insertions(+), 156 deletions(-) create mode 100644 readme.adoc delete mode 100644 src/main/resources/camel-route.xml diff --git a/conf/springboot.yml b/conf/springboot.yml index 74be0c2..2f777d7 100644 --- a/conf/springboot.yml +++ b/conf/springboot.yml @@ -1,2 +1,5 @@ server: - port: 9090 \ No newline at end of file + port: 9090 +camel: + springboot: + main-run-controller: true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5816e03..4fdee0d 100644 --- a/pom.xml +++ b/pom.xml @@ -15,73 +15,66 @@ UTF-8 17 17 - 4.7.0 - 2.17.2 + 4.8.2 + 4.0.6 ${project.parent.version} - - - org.apache.camel.springboot - camel-spring-boot-bom - ${camel.version} - pom - import - - - org.apache.camel - camel-bom - ${camel.version} - pom - import - - - com.fasterxml.jackson - jackson-bom - ${jackson.version} - pom - import - - - + + + org.apache.camel.springboot + camel-spring-boot-bom + ${camel.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${springboot.version} + pom + import + + + + - - org.apache.camel.springboot - camel-spring-boot-xml-starter - - - org.apache.camel - camel-jsonpath - - - org.apache.camel - camel-bean-validator - - - org.apache.camel - camel-cxf-rest - - - com.fasterxml.jackson.jakarta.rs - jackson-jakarta-rs-json-provider - - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - - - ch.qos.logback - logback-core - - - jakarta.ws.rs - jakarta.ws.rs-api - + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.camel.springboot + camel-log-starter + + + org.apache.camel.springboot + camel-bean-starter + + + org.apache.camel.springboot + camel-bean-validator-starter + + + org.apache.camel.springboot + camel-cxf-rest-starter + + + + + com.fasterxml.jackson.jakarta.rs + jackson-jakarta-rs-json-provider + + + + + org.apache.cxf + cxf-rt-rs-service-description + ${cxf.version} + diff --git a/readme.adoc b/readme.adoc new file mode 100644 index 0000000..fe81bd5 --- /dev/null +++ b/readme.adoc @@ -0,0 +1,91 @@ +== Spring Boot Example with Camel exposing REST services using Apache CXF + +=== Introduction + +This example illustrates how to use https://projects.spring.io/spring-boot/[Spring Boot] with http://camel.apache.org[Camel]. It provides a simple REST service that is created using https://cxf.apache.org/[Apache CXF]. + + +=== Build + +You can build this example using: + + $ mvn package + +=== Run + +You can run this example using: + + $ mvn spring-boot:run + +After the Spring Boot application is started, you can open the following URL in your web browser to access the list of services: http://localhost:8080/services/ including WADL definition + +You can also access the REST endpoint from the command line: + +List all the users +[source,text] +---- +$ curl http://localhost:8080/services/api/user -s | jq . +---- + +The command will produce the following output: + +[source,json] +---- +[ { + "id" : 1, + "name" : "John Coltrane" +}, { + "id" : 2, + "name" : "Miles Davis" +}, { + "id" : 3, + "name" : "Sonny Rollins" +} ] +---- + +Retrieve a specific user +[source,text] +---- +$ curl http://localhost:8080/services/api/user/1 -s | jq . +---- + +The command will produce the following output: + +[source,json] +---- +{ + "id": 1, + "name": "John Coltrane" +} +---- + +Insert/update user + +[source,text] +---- +$ curl -X PUT http://localhost:8080/services/api/user --data '{"id":4,"name":"Charlie Parker"}' -H 'Content-Type: application/json' -v +---- + +The http status code of the response will be https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml#http-status-codes-1[201] + +Moreover, the input user is validated according to the annotations on the link:src/main/java/org/apache/camel/example/springboot/cxf/User.java[User bean] + +[source,text] +---- +$ curl -X PUT http://localhost:8080/services/api/user --data '{"id":4,"name":"C"}' -H 'Content-Type: application/json' +---- + +will produce a validation error + + +The Spring Boot application can be stopped pressing `[CTRL] + [C]` in the shell. + +=== Help and contributions + +If you hit any problem using Camel or have some feedback, then please +https://camel.apache.org/community/support/[let us know]. + +We also love contributors, so +https://camel.apache.org/community/contributing/[get involved] :-) + +The Camel riders! \ No newline at end of file diff --git a/src/main/java/com/example/camel/Boot.java b/src/main/java/com/example/camel/Boot.java index 8323eed..abbc384 100644 --- a/src/main/java/com/example/camel/Boot.java +++ b/src/main/java/com/example/camel/Boot.java @@ -6,10 +6,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ImportResource; @SpringBootApplication -@ImportResource("classpath:camel-route.xml") public class Boot { private static final Logger logger = LoggerFactory.getLogger(Boot.class); diff --git a/src/main/java/com/example/camel/CamelRouter.java b/src/main/java/com/example/camel/CamelRouter.java index 0d5c4f4..2879196 100644 --- a/src/main/java/com/example/camel/CamelRouter.java +++ b/src/main/java/com/example/camel/CamelRouter.java @@ -3,6 +3,7 @@ package com.example.camel; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.bean.validator.BeanValidationException; + import org.springframework.stereotype.Component; import jakarta.ws.rs.core.Response; @@ -12,6 +13,7 @@ public class CamelRouter extends RouteBuilder { @Override public void configure() throws Exception { + //very raw way, just to handle the validation responses onException(BeanValidationException.class) .handled(true) .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(Response.Status.BAD_REQUEST.getStatusCode())) @@ -19,7 +21,7 @@ public class CamelRouter extends RouteBuilder { // @formatter:off from("cxfrs:/api?" + - "resourceClasses=com.example.camel.UserService" + + "resourceClasses="+UserService.class.getName() + "&bindingStyle=SimpleConsumer" + "&providers=jaxrsProvider" + "&loggingFeatureEnabled=true") diff --git a/src/main/resources/camel-route.xml b/src/main/resources/camel-route.xml deleted file mode 100644 index ef6b558..0000000 --- a/src/main/resources/camel-route.xml +++ /dev/null @@ -1,90 +0,0 @@ - - - - - - - - - - ${headers.CamelHttpMethod} == 'POST' - - $.notification.type - - - - ${header.type} == 'email' - - - - 200 - - - - ${header.type} == 'http' - - - - 200 - - - - - - { "status": "reject", "type": "unknown" } - - - 400 - - - - - - - - { "error": "only POST is accepted" } - - - 500 - - - - - - - - - $.notification.to - - - Notification - - - $.notification.message - - - - { "status": "email sent", "to": "${header.to}", "subject": - "${header.subject}" } - - - - - - - $.notification.service - - - - { "status": "http requested", "service": "${header.service}" - } - - - - \ No newline at end of file