Apache CXF with JAX-WS

Apache CXF with JAX-WS ”; Previous Next In this JAX-WS application, we will use Apache CXF-first approach like the earlier POJO application. So first we will create an interface for our web service. Declaring Service Interface As in the earlier case, we will create a trivial service that has only one interface method called greetings. The code for the service interface is shown below − //HelloWorld.java package com.tutorialspoint.cxf.jaxws.helloworld; import javax.jws.WebService; @WebService public interface HelloWorld { String greetings(String text); } We annotate the interface with a @WebService tag. Next, we will implement this interface. Implementing Web Interface The implementation of the web interface is shown here − //HelloWorldImpl.java package com.tutorialspoint.cxf.jaxws.helloworld; public class HelloWorldImpl implements HelloWorld { @Override public String greetings(String name) { return (“hi ” + name); } } The greetings method is annotated with @Override tag. The method returns a “hi” message to the caller. Next, we will write the code for developing the server. Developing Server Unlike the POJO application, we will now decouple the interface by using the CXF supplied Endpoint class to publish our service. This is done in the following two lines of code − HelloWorld implementor = new HelloWorldImpl(); Endpoint.publish( “http://localhost:9090/HelloServerPort”, implementor, new LoggingFeature() ); The first parameter of the publish method specifies the URL at which our service will be made available to the clients. The second parameter specifies the implementation class for our service. The entire code for the server is shown below − //Server.java package com.tutorialspoint.cxf.jaxws.helloworld; import javax.xml.ws.Endpoint; import org.apache.cxf.ext.logging.LoggingFeature; public class Server { public static void main(String[] args) throws Exception { HelloWorld implementor = new HelloWorldImpl(); Endpoint.publish(“http://localhost:9090/HelloServerPort”, implementor, new LoggingFeature()); System.out.println(“Server ready…”); Thread.sleep(5 * 60 * 1000); System.out.println(“Server exiting …”); System.exit(0); } } To deploy our server, you will need to make few more modifications to your project as listed below. Deploying Server Finally, to deploy the server application, you will need to make one more modification in pom.xml to setup your application as a web application. The code that you need to add into your pom.xml is given below − <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.jaxws.helloworld.Server </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> Before you deploy the application, you need to add two more files to your project. These are shown in the screenshot below − These files are CXF standard files which define the mapping for CXFServlet. The code within the web.xml file is shown here for your quick reference − //Web.xml <?xml version = “1.0” encoding = “UTF-8”??> <web-app xmlns = “http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” version=”2.5″ xsi:schemaLocation = “http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”> <display-name>cxf</display-name> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> cxf </servlet-name> <url-pattern> /services/* </url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app> In the cxf-servlet.xml, you declare the properties for your service”s endpoint. This is shown in the code snippet below − <beans …> <jaxws:endpoint xmlns:helloworld = “https://www.tutorialspoint.com/” id = “helloHTTP” address = “http://localhost:9090/HelloServerPort” serviceName = “helloworld:HelloServiceService” endpointName = “helloworld:HelloServicePort”> </jaxws:endpoint> </beans> Here we define the id for our service endpoint, the address on which the service will be available, the service name and the endpoint name. Now, you learnt how your service gets routed and processed by a CXF servlet. The Final pom.xml The pom.xml includes a few more dependencies. Rather than describing all the dependencies, 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-jaxws</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.jaxws.helloworld.Server </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </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> com.tutorialspoint.cxf.jaxws.helloworld.Client </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.0</version> </dependency> <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-features-logging</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> </dependencies> </project> Note that it also includes a profile for building client that we will be learning in the later sections of this tutorial. Running the HelloWorld Service Now, you are ready to run the web app. In the command window, run the build script using the following command. mvn clean install mvn -Pserver You will see the following message on the console − INFO: Setting the server”s publish address to be http://localhost:9090/HelloServerPort Server ready… Like earlier, you can test the server by opening the server URL in your browser. As we did not specify any operation, only a fault message is returned to the browser by our application. Now, try adding the ?wsdl to your URL and you will see the following output − So our server application is running as expected. You may use the SOAP Client such as Postman described earlier to further test your service. In the next section, we will learn how to write a client that uses our service. Developing Client Writing the client in a CXF application is as trivial as writing a server. Here is the complete code for the client − //Client.java package com.tutorialspoint.cxf.jaxws.helloworld; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.soap.SOAPBinding; public final class Client { private static final QName SERVICE_NAME = new QName(“http://helloworld.jaxws.cxf.tutorialspoint.com/”, “HelloWorld”); private static final QName PORT_NAME = new QName(“http://helloworld.jaxws.cxf.tutorialspoint.com/”, “HelloWorldPort”); private Client() { } public static void main(String[] args) throws Exception { Service service = Service.create(SERVICE_NAME); System.out.println(“service created”); String endpointAddress = “http://localhost:9090/HelloServerPort”; service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); HelloWorld hw = service.getPort(HelloWorld.class); System.out.println(hw.greetings(“World”)); } } Here, we use the CXF supplied Service class to bind to the known service. We call the create method on the Service class to get an instance of the service. We set the known port by calling the addPort method on the service instance. Now, we are ready to consume the service, which we do by first obtaining the service interface by calling the getPort method on the service instance. Finally, we call our greetings method

Apache CXF with POJO

Apache CXF with POJO ”; Previous Next In this chapter, you will learn how to develop a simple web application that sends a greetings message to the user. A web service project uses WSDL model. The CXF allows you to hide this WSDL model by providing a simple frontend to map Apache CXF APIs to the underlying WSDL. In this simplest project, the interface of the web service will be directly exposed to the client and the client would use native Apache CXF APIs to call the web service. First, we will create a web service. Every service has an interface that is exposed to the client. We may write this interface as a simple Apache CXF interface or as a WSDL document. In this Apache CXF-First approach, we will expose our service through a Apache CXF interface. Developing Web Service The service that we are going to create on the web will have a single web method called greetings. The method takes a string type argument in which we will send the user”s name. The service will send back a greetings message to the caller with the received user name included in the message. Web Service Interface To expose the interface of our web service, we will create a Apache CXF interface as follows − //HelloWorld.java package com.tutorialspoint.cxf.pojo; public interface HelloWorld { String greetings(String text); } The interface has only one method called greetings. The server will implement this interface. In our trivial application, this interface is directly exposed to the client. Typically, in a web service application, you use WSDL to describe the web service interface. In this simple application, we will provide this direct interface to the client developer. The client would then call the greetings message on the server object. So first let us create the web service. Web Service Implementation The HelloWorld interface is implemented in the HelloWorldImpl Apache CXF class as shown below − //HelloWorldImpl.java package com.tutorialspoint.cxf.pojo; public class HelloWorldImpl implements HelloWorld { @Override public String greetings(String text) { return “Hi ” + text; } } The greetings method receives a parameter of string type, appends it to a greeting message and returns the resultant string to the caller. Next, we write the server application to host the HelloWorld service. Creating Server The server application consists of two parts − The first part creates a factory for our web service, and The second part writes a main method for instantiating it. The server uses ServerFactoryBean class provided by CXF libraries to expose our HelloWorld interface to remote clients. Thus, we first instantiate the ServerFactoryBean class and then set its various properties − ServerFactoryBean factory = new ServerFactoryBean(); We set the service class to be called by calling the setServiceClass method on the factory object − factory.setServiceClass(HelloWorld.class); We set the URL for calling our service by calling the factory”s setAddress method. Note that the service will be published at this URL. factory.setAddress(“http://localhost:5000/Hello”); In this case, the service is deployed on the embedded server and will be listening to port 5000. You may opt for any port number of your choice. Before creating the factory, you need to tell the factory about our service implementation class. This is done by calling the setServiceBean method on the factory object as shown here − factory.setServiceBean(new HelloWorldImpl()); The service bean is set to the instance of our service implementation class. Finally, we create the factory by calling its create method − factory.create(); Now, as we have developed the factory to run our web service, we will next write a main method to instantiate it and keep it running for some time. Now, write a main method to instantiate the HelloServer class as follows − public static void main(String[] args) throws Exception { new HelloServer(); System.out.println(“Listening on port 5000 …”); } Once instantiated, the HelloServer class will keep running indefinitely. For production deployments, you will definitely keep your server running forever. In the current situation, we would terminate the server after a predetermined time as follows − Thread.sleep(5 * 60 * 1000); System.out.println(“Server exiting …”); System.exit(0); The entire code for the HelloServer class is given below − //HelloServer.java //HelloServer.java package com.tutorialspoint.cxf.pojo; import org.apache.cxf.frontend.ServerFactoryBean; public class HelloServer { protected HelloServer() throws Exception { ServerFactoryBean factory = new ServerFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress(“http://localhost:5000/Hello”); factory.setServiceBean(new HelloWorldImpl()); factory.create(); } public static void main(String[] args) throws Exception { new HelloServer(); System.out.println(“Listening on port 5000 …”); Thread.sleep(5 * 60 * 1000); System.out.println(“Server exiting …”); System.exit(0); } } The server application that we have created uses ServerFactoryBean class from CXF libraries. We must now include these libraries in our project to successfully compile the HelloServer class. We will use Maven to set up the project dependencies. Setting up Maven Project To create a Maven project, type the following command in your command-line window. Note that we have tested this on a Mac machine. For Windows and Linux installations, the instructions may differ at few places. mvn archetype:generate When asked for the properties, input the following values − Define value for property ”groupId”: : com.tutorialspoint Define value for property ”artifactId”: : cxf-pojo Define value for property ”version”: 1.0-SNAPSHOT: : 1.0 Define value for property ”package”: com.tutorialspoint: : com.tutorialspoint.cxf.pojo On the completion of the maven command, you will find the appropriate folder structure created in your current folder along with pom.xml file. The generated directory structure is shown here − You will add the CXF dependencies in the pom.xml and also copy the above created Apache CXF files into the appropriate folder of the maven created structure. For your ready reference, we have given below the pom.xml file for the project that we created on our machine. <?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-pojo</artifactId> <version>1.0</version> <packaging>jar</packaging> <profiles> <profile> <id>server</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.pojo.HelloServer </mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </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>