English 中文(简体)
Apache CXF with JAX-RS
  • 时间:2024-09-17

Apache CXF with JAX-RS


Previous Page Next Page  

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 pst 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 cpent. In our trivial case, we will simply return the movie name to the cpent and not the actual binary MP4 file. So let us start creating a JAX-RS apppcation.

Declaring Movie Element

We will declare an XML root element called Movie for storing the id and the name for a given movie. The element is declared in a file called Movie.java. The contents of the file are shown here −

//Movie.java
package com.tutorialspoint.cxf.jaxrs.movie;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Movie")
pubpc class Movie {
   private long id;
   private String name;
   pubpc long getId() {
      return id;
   }
   pubpc void setId(long id) {
      this.id = id;
   }
   pubpc String getName() {
      return name;
   }
   pubpc 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 pst of movies in its database.

Creating Movie Service Database

To store the pst of movies we use Java suppped Map that stores the key-value pairs. If the pst 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")
pubpc class MovieService {
   long currentId = 123;
   Map<Long, Movie> movies = new HashMap<>();
   pubpc MovieService() {
      init();
   }
   @GET
   @Path("/movie/{id}/")
   pubpc 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);
      
      Movie c2 = new Movie();
      c2.setName("Mission Imposssible");
      c2.setId(1002);
      movies.put(c2.getId(), c2);
      
      Movie c3 = new Movie();
      c3.setName("Black Panther");
      c3.setId(1003);
      movies.put(c3.getId(), c3);
      
      Movie c4 = new Movie();
      c4.setName("A Star is Born");
      c4.setId(1004);
      movies.put(c4.getId(), c4);
      
      Movie c5 = new Movie();
      c5.setName("The Meg");
      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 initiapzed in the init method, where we add five movie items to the database.

Our next task is to write a server apppcation.

Developing Server

To create a server, we use CXF suppped JAXRSServerFactoryBean class.

JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();

We set its resource classes by calpng the setResourceClasses method.

factory.setResourceClasses(Movie.class);
factory.setResourceClasses(MovieService.class);

We set the service provider by calpng the setResourceProvider method.

factory.setResourceProvider(MovieService.class,
new SingletonResourceProvider(new MovieService()));

We set the desired pubpsh address by calpng the aetAddress method −

factory.setAddress("http://localhost:9000/");

Finally, we pubpsh the server by calpng the create method on the factory instance.

factory.create();

The entire code for the server apppcation is given below −

//Server.java
package com.tutorialspoint.cxf.jaxrs.movie;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.pfecycle.SingletonResourceProvider;
pubpc class Server {
   pubpc 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);
      
      System.out.println("Server exiting ...");
      System.exit(0);
   }
}

The Final pom.xml

Here we have included the final version of pom.xml below −

<?xml version = "1.0" encoding = "UTF-8"?>
<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>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>cxf-jaxrs</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>
   <profiles>
      <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>cpent</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>
                              com.tutorialspoint.cxf.jaxrs.movie.Cpent
                           </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>httpcpent</artifactId>
         <version>4.5.7</version>
      </dependency>
   </dependencies>
</project>

Developing Cpent

Writing the RS cpent is trivial. We simply create a URL object and open its stream. We use CXF suppped IOUtils class to copy the contents of input stream to a local stream.

URL url = new URL("http://localhost:9000/movieservice/movie/1002");
try (InputStream instream = url.openStream();
CachedOutputStream outstream = new CachedOutputStream()) {
   IOUtils.copy(instream, outstream);
}

The entire code for the cpent apppcation is given below −

//Cpent.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;
pubpc class Cpent {
   pubpc 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);
      }
   }
}

Testing JAX-RS Apppcation

Run the server using the following command in the command-pne window −

mvn -Pserver

Now, you will see the following message on the console −

INFO: Setting the server s pubpsh address to be http://localhost:9000

Now, open your browser and type the following URL −

http://localhost:9000/movieservice/movie/1002

You will see the following in the browser window.

browser window

You may invoke the service by using a Java cpent apppcation that we have developed by running the following command in a separate command-pne window.

mvn -Pcpent

You will see the following output −

<?xml version="1.0" encoding = "UTF-8" standalone="yes"?>
<Movie><id>1002</id><name>Mission Imposssible</name></Movie>

CXF samples provides several examples on how to use CXF with JAX-RS. The interested readers are encouraged to study these samples.

Advertisements