Apache CXF With JAX-RS: Declaring Movie Element
Apache CXF With JAX-RS: Declaring Movie Element
Before proceeding ahead into this chapter, we assume that you know how to write a
RESTful web service in Java. I will show you how to use CXF on top of this JAX-RS (Java
API for RESTful Web Services) . We will create a web service that maintains a list of latest
movies. When the user requests a movie, he specifies the movie ID in his request, the
server will locate the movie and return it to the client. In our trivial case, we will simply return
the movie name to the client and not the actual binary MP4 file. So let us start creating a
JAX-RS application.
//Movie.java
package com.tutorialspoint.cxf.jaxrs.movie;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Movie")
public class Movie {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Note the use of XmlRootElement tag to declare the XML element for the Movie tag. Next,
we will create a service that holds the list of movies in its database.
1 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
list is large, you will use an external database storage which will also be easier to manage.
In our trivial case, we will store only five movies in our database. The code for the
MovieService class is given below −
//MovieService.java
package com.tutorialspoint.cxf.jaxrs.movie;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("/movieservice/")
@Produces("text/xml")
public class MovieService {
long currentId = 123;
Map<Long, Movie> movies = new HashMap<>();
public MovieService() {
init();
}
@GET
@Path("/movie/{id}/")
public Movie getMovie(@PathParam("id") String id) {
long idNumber = Long.parseLong(id);
return movies.get(idNumber);
}
final void init() {
Movie c1 = new Movie();
c1.setName("Aquaman");
c1.setId(1001);
movies.put(c1.getId(), c1);
2 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
c5.setId(1005);
movies.put(c5.getId(), c5);
}
}
Note that we use the following two annotations to specify the URL path for our movie service
and its return type −
@Path("/movieservice/")
@Produces("text/xml")
We use the @GET and @Path annotations to specify the URL for the GET request as
follows −
@GET
@Path("/movie/{id}/")
The movie database itself is initialized in the init method, where we add five movie items to
the database.
Developing Server
To create a server, we use CXF supplied JAXRSServerFactoryBean class.
factory.setResourceClasses(Movie.class);
factory.setResourceClasses(MovieService.class);
factory.setResourceProvider(MovieService.class,
new SingletonResourceProvider(new MovieService()));
factory.setAddress("http://localhost:9000/");
Finally, we publish the server by calling the create method on the factory instance.
3 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
factory.create();
//Server.java
package com.tutorialspoint.cxf.jaxrs.movie;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
public class Server {
public static void main(String[] args) throws Exception {
JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setResourceClasses(Movie.class);
factory.setResourceClasses(MovieService.class);
factory.setResourceProvider(MovieService.class,
new SingletonResourceProvider(new MovieService()));
factory.setAddress("http://localhost:9000/");
factory.create();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
4 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.jaxrs.movie.Server
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
5 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
com.tutorialspoint.cxf.jaxrs.movie.Client
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
</dependencies>
</project>
Developing Client
Writing the RS client is trivial. We simply create a URL object and open its stream. We use
CXF supplied IOUtils class to copy the contents of input stream to a local stream.
6 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
IOUtils.copy(instream, outstream);
}
//Client.java
package com.tutorialspoint.cxf.jaxrs.movie;
import java.io.InputStream;
import java.net.URL;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
public class Client {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:9000/movieservice/movie/1002");
try (InputStream instream = url.openStream();
CachedOutputStream outstream = new CachedOutputStream()) {
IOUtils.copy(instream, outstream);
String str = outstream.getOut().toString();
System.out.println(str);
}
}
}
mvn -Pserver
http://localhost:9000/movieservice/movie/1002
7 of 8 03/04/20, 5:47 pm
Apache CXF with JAX-RS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
You may invoke the service by using a Java client application that we have developed by
running the following command in a separate command-line window.
mvn -Pclient
CXF samples provides several examples on how to use CXF with JAX-RS. The interested
readers are encouraged to study these samples.
8 of 8 03/04/20, 5:47 pm