https://github.com/hedleyproctor/cxf-restful-server-example
This example shows the key steps in creating a restful web server:
- Create a rest service interface class which you annotate with the RS annotation @WebService.
- Your rest service interface class contains a method signature for each operation. Each one is annotated to say what its path is and the data format for request and response.
- You write an implementation class that contains the code to be executed when each endpoint is hit.
- In Spring XML configuration, you define the server endpoint, configuring it with your rest service interface(s) and any necessary data conversion classes.
So in my example, the rest interface looks like this:
package org.example; import com.ice.fraud.Claim; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.jws.WebService; @Path("/hello") @WebService public interface HelloWorldRestService { @GET @Path("/greet") @Produces(MediaType.TEXT_PLAIN) public Response greet(); @POST @Path("/sayhello") @Produces(MediaType.APPLICATION_JSON) public Response sayHello(String input); @POST @Path("/submit") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response submit(Claim claim); }
Then each method is implemented in the implementation class, to return the appropriate data and an http response code:
public class HelloWorldRestServiceImpl implements HelloWorldRestService { public Response greet() { return Response.status(Status.OK). entity("Hi There!!"). build(); }
The server definition is in a Spring xml configuration file called cxf-beans.xml. You give CXF a list of all your interface classes, and what data providers you need to use. In this example, the data format is json, so the data provider is the JacksonJsonProvider class.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="helloWorldRestService" class="org.example.HelloWorldRestServiceImpl" /> <!-- By default, when CXF starts a server it starts an instance of the Jetty java web server. --> <!-- The endpoint for a CXF operation is composed of three sections: --> <!-- 1. base URL - defined here --> <!-- 2. service path - defined by the @Path annotation at the top of your service class --> <!-- 3. operation path - defined by the @Path annotation on each method --> <cxf:server id="helloServer" address="http://localhost:8080/cxf-rest"> <cxf:serviceBeans> <ref bean="helloWorldRestService" /> </cxf:serviceBeans> <cxf:providers> <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" /> </cxf:providers> </cxf:server> </beans>
The repo also contains an integration service written in JUnit 5 which shows how to test the services using an Apache http client.
For other Java integration topics, see:
Error handling in Apache Camel
Type conversion exceptions in Apache Camel