V0.0..2 Added some camel-cxf-rs code for testing

master
Ng Yat Yan 4 months ago
parent 58de3d3b14
commit 2d07aff6b2

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <groupId>com.example</groupId>
<artifactId>camel-springboot-xml-example</artifactId> <artifactId>camel-springboot-xml-example</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
<name>camel-springboot-xml-example</name> <name>camel-springboot-xml-example</name>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -13,7 +13,10 @@
</parent> </parent>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<camel.version>4.7.0</camel.version> <camel.version>4.7.0</camel.version>
<jackson.version>2.17.2</jackson.version>
<springboot.version>${project.parent.version}</springboot.version> <springboot.version>${project.parent.version}</springboot.version>
</properties> </properties>
@ -33,6 +36,13 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
@ -44,6 +54,18 @@
<groupId>org.apache.camel</groupId> <groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId> <artifactId>camel-jsonpath</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bean-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf-rest</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jakarta.rs</groupId>
<artifactId>jackson-jakarta-rs-json-provider</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
@ -56,6 +78,10 @@
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId> <artifactId>logback-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
@ -63,8 +89,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>17</source> <source>${maven.compiler.source}</source>
<target>17</target> <target>${maven.compiler.target}</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

@ -0,0 +1,33 @@
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;
@Component
public class CamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
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=com.example.camel.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
}
}

@ -0,0 +1,15 @@
package com.example.camel;
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();
}
}

@ -0,0 +1,52 @@
package com.example.camel;
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();
}
}

@ -0,0 +1,53 @@
package com.example.camel;
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);
}

@ -0,0 +1,35 @@
package com.example.camel;
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();
}
}
Loading…
Cancel
Save