This is an automated email from the ASF dual-hosted git repository. fmariani pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-spring-boot-examples.git
The following commit(s) were added to refs/heads/main by this push: new 9bfb5cc CAMEL-19886: Add rest-cxf example 9bfb5cc is described below commit 9bfb5cc1233464c6857455921a225e4657c737c5 Author: Marco Carletti <mcarl...@redhat.com> AuthorDate: Tue Sep 19 22:18:33 2023 +0200 CAMEL-19886: Add rest-cxf example --- README.adoc | 2 + pom.xml | 1 + rest-cxf/README.adoc | 91 +++++++++++++++++ rest-cxf/pom.xml | 113 +++++++++++++++++++++ .../camel/example/springboot/cxf/Application.java | 34 +++++++ .../camel/example/springboot/cxf/CamelRouter.java | 51 ++++++++++ .../camel/example/springboot/cxf/CxfConfig.java | 31 ++++++ .../apache/camel/example/springboot/cxf/User.java | 68 +++++++++++++ .../camel/example/springboot/cxf/UserService.java | 69 +++++++++++++ .../example/springboot/cxf/UserServiceImpl.java | 51 ++++++++++ rest-cxf/src/main/resources/application.properties | 21 ++++ 11 files changed, 532 insertions(+) diff --git a/README.adoc b/README.adoc index b53c8ac..fa3634b 100644 --- a/README.adoc +++ b/README.adoc @@ -127,6 +127,8 @@ Number of Examples: 49 (0 deprecated) | link:platform-http/README.adoc[Platform Http] (platform-http) | Rest | An example showing Camel REST DSL with platform HTTP +| link:rest-cxf/README.adoc[Rest Cxf] (rest-cxf) | Rest | An example showing Camel REST using CXF with Spring Boot + | link:rest-openapi/README.adoc[Rest Openapi] (rest-openapi) | Rest | An example showing Camel REST DSL and OpenApi with Spring Boot | link:rest-openapi-simple/README.adoc[REST OpenApi] (rest-openapi-simple) | Rest | This example shows how to call a Rest service defined using OpenApi specification diff --git a/pom.xml b/pom.xml index 420ded6..8d63595 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ <module>rabbitmq</module> <module>reactive-streams</module> <module>resilience4j</module> + <module>rest-cxf</module> <module>rest-openapi</module> <module>rest-openapi-simple</module> <module>rest-openapi-springdoc</module> diff --git a/rest-cxf/README.adoc b/rest-cxf/README.adoc new file mode 100644 index 0000000..111e61c --- /dev/null +++ b/rest-cxf/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! diff --git a/rest-cxf/pom.xml b/rest-cxf/pom.xml new file mode 100644 index 0000000..82c738e --- /dev/null +++ b/rest-cxf/pom.xml @@ -0,0 +1,113 @@ +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<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> + + <parent> + <groupId>org.apache.camel.springboot.example</groupId> + <artifactId>examples</artifactId> + <version>4.1.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-rest-cxf</artifactId> + <name>Camel SB Examples :: REST using CXF</name> + <description>An example showing Camel REST using CXF with Spring Boot</description> + + <properties> + <category>Rest</category> + </properties> + + <!-- Spring-Boot and Camel BOM --> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-dependencies</artifactId> + <version>${spring-boot-version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <groupId>org.apache.camel.springboot</groupId> + <artifactId>camel-spring-boot-bom</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <!-- Spring Boot --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <!-- Camel --> + <dependency> + <groupId>org.apache.camel.springboot</groupId> + <artifactId>camel-log-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.springboot</groupId> + <artifactId>camel-bean-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.springboot</groupId> + <artifactId>camel-bean-validator-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.springboot</groupId> + <artifactId>camel-cxf-rest-starter</artifactId> + </dependency> + + <!-- jax-rs json provider from jackson --> + <dependency> + <groupId>com.fasterxml.jackson.jakarta.rs</groupId> + <artifactId>jackson-jakarta-rs-json-provider</artifactId> + </dependency> + + <!-- CXF WADL definition--> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-rs-service-description</artifactId> + <version>${cxf-version}</version> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring-boot-version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> diff --git a/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/Application.java b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/Application.java new file mode 100644 index 0000000..7437d55 --- /dev/null +++ b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/Application.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.springboot.cxf; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +// CHECKSTYLE:OFF +@SpringBootApplication +public class Application { + + /** + * Main method to start the application. + */ + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} +// CHECKSTYLE:ON diff --git a/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/CamelRouter.java b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/CamelRouter.java new file mode 100644 index 0000000..fe8b447 --- /dev/null +++ b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/CamelRouter.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.springboot.cxf; + +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; + +@Component +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())) + .setBody(simple("${exchangeProperty.CamelExceptionCaught.getMessage()}")); + + // @formatter:off + from("cxfrs:/api?" + + "resourceClasses=org.apache.camel.example.springboot.cxf.UserService" + + "&bindingStyle=SimpleConsumer" + + "&providers=jaxrsProvider" + + "&loggingFeatureEnabled=true") + .to("bean-validator:user") + .to("log:camel-cxf-log?showAll=true") + .setHeader(Exchange.BEAN_METHOD_NAME, simple("${header.operationName}")) + .bean(UserServiceImpl.class); + // @formatter:on + } + +} diff --git a/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/CxfConfig.java b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/CxfConfig.java new file mode 100644 index 0000000..b15be0d --- /dev/null +++ b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/CxfConfig.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.springboot.cxf; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider; + +@Configuration +public class CxfConfig { + + @Bean + public JacksonJsonProvider jaxrsProvider() { + return new JacksonJsonProvider(); + } +} diff --git a/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/User.java b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/User.java new file mode 100644 index 0000000..c9356fd --- /dev/null +++ b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/User.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.springboot.cxf; + +import java.util.StringJoiner; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; + +/** + * User entity + * + */ +public class User { + + @NotNull(message = "custom message") + private Integer id; + + @NotNull + @Size(min = 3, max = 20) + private String name; + + public User() { + } + + public User(Integer id, String name) { + this.id = id; + this.name = 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; + } + + @Override + public String toString() { + return new StringJoiner(", ", User.class.getSimpleName() + "[", "]") + .add("id=" + id) + .add("name='" + name + "'") + .toString(); + } +} diff --git a/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/UserService.java b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/UserService.java new file mode 100644 index 0000000..0241add --- /dev/null +++ b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/UserService.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.springboot.cxf; + +import java.util.Collection; + +import jakarta.validation.Valid; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +/** + * Service interface for managing users. + */ +public interface UserService { + + /** + * Find a user by the given ID + * + * @param id + * the ID of the user + * @return the user, or <code>null</code> if user not found. + */ + @GET + @Path("/user/{id}") + @Produces(MediaType.APPLICATION_JSON) + User findUser(@PathParam("id") Integer id); + + /** + * Find all users + * + * @return a collection of all users + */ + @GET + @Path("/user") + @Produces(MediaType.APPLICATION_JSON) + Collection<User> findUsers(); + + /** + * Update the given user + * + * @param user + * the user + */ + @PUT + @Path("/user") + @Consumes(MediaType.APPLICATION_JSON) + Response updateUser(@Valid User user); + +} diff --git a/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/UserServiceImpl.java b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/UserServiceImpl.java new file mode 100644 index 0000000..a5482da --- /dev/null +++ b/rest-cxf/src/main/java/org/apache/camel/example/springboot/cxf/UserServiceImpl.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.springboot.cxf; + +import java.util.Collection; +import java.util.Map; +import java.util.TreeMap; + +import jakarta.ws.rs.core.Response; + +public class UserServiceImpl implements UserService { + + private final Map<Integer, User> users = new TreeMap<>(); + + public UserServiceImpl() { + users.put(1, new User(1, "John Coltrane")); + users.put(2, new User(2, "Miles Davis")); + users.put(3, new User(3, "Sonny Rollins")); + } + + @Override + public User findUser(Integer id) { + return users.get(id); + } + + @Override + public Collection<User> findUsers() { + return users.values(); + } + + @Override + public Response updateUser(User user) { + users.put(user.getId(), user); + return Response.noContent().status(Response.Status.CREATED).build(); + } + +} diff --git a/rest-cxf/src/main/resources/application.properties b/rest-cxf/src/main/resources/application.properties new file mode 100644 index 0000000..0097a49 --- /dev/null +++ b/rest-cxf/src/main/resources/application.properties @@ -0,0 +1,21 @@ +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You under the Apache License, Version 2.0 +## (the "License"); you may not use this file except in compliance with +## the License. You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## --------------------------------------------------------------------------- + +# the name of Camel +camel.springboot.name = MyCamel +camel.springboot.main-run-controller=true +#logging.level.org.apache.cxf = TRACE