Apache CXF: An Introduction to Web Service RPC Framework

Keywords: Java Apache Maven xml Spring

This blog Uncle Cat's Blog For reprinting, please state your origin

Learning Series

Apache CXF: An Introduction to WebService RPC Framework

CXF official website: http://cxf.apache.org/

Apache CXF is an open source Web Service RPC framework, which is a combination of Celtix and Codhaus XFire. It can be said to be a fully functional collection.

Functional features:

  • Supporting Web Service standards, including SOAP (1.1, 1.2) specifications, WSI Basic Profile... etc. I don't know. Here are not examples.
  • Supports JSR-related specifications and standards, including.... Ibid.
  • Support multiple transport protocols and protocol binding (SOAP, REST/HTTP, XML), data binding (JAXB2.X, Aegis, Apache XML Beans).

Let's start with a case study.

Project source address: RPC_Demo Remember the comgithubcxf in the project

  • 1. Using IDEA to build a maven project, I chose maven-archetype-webapp to build the basic framework. Of course, you may need to create some directories as well.

  • 2. I think it's time to configure the main pom files first.
<?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>cxf</groupId>
  <artifactId>comgithubcxf</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>comgithubcxf Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <cxf.version>3.1.7</cxf.version>
    <spring.version>4.0.9.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>comgithubcxf</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
  • 3. There is also a service implementation to build the Server side. The interface uses the @WebService annotation to indicate that it is a Web Service remote service interface.
package com.github.cxf.server;

import javax.jws.WebService;

/**
 * Create by UncleCatMySelf in 21:57 2019\4\23 0023
 */
@WebService
public interface CxfService {

    String say(String someOne);

}

The same is added to the implementation class, and the interface implementation is marked by endpoint interface.

package com.github.cxf.server;

import javax.jws.WebService;

/**
 * Create by UncleCatMySelf in 21:57 2019\4\23 0023
 */
@WebService(endpointInterface = "com.github.cxf.server.CxfService")
public class CxfServiceImpl implements CxfService {
    @Override
    public String say(String someOne) {
        return someOne + ",Welcome to Study!";
    }
}
  • 4. Write the corresponding cxf-server.xml file (core point), here I refer to the case of the official website.
<?xml version="1.0" encoding="UTF-8"?>
<!--
        Licensed to the Apache Software Foundation (ASF) under one
        or more contributor license agreements. See the NOTICE file
        distributed with this work for additional information
        regarding copyright ownership. The ASF licenses this file
        to you under the Apache License, Version 2.0 (the
        "License"); you may not use this file except in compliance
        with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing,
        software distributed under the License is distributed on an
        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
        KIND, either express or implied. See the License for the
        specific language governing permissions and limitations
        under the License.
-->
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <jaxws:endpoint id="helloWorld" implementor="com.github.cxf.server.CxfServiceImpl" address="/server"/>
</beans>
        <!-- END SNIPPET: beans -->
  • 5. Then we have our web.xml file.
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:cxf-server.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>CXFServer</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServer</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>
  • 6. Configure tomcat. Because I am an IDEA environment, I will show you the screenshots.


Then start tomcat. If it's normal together, godmother will bless you! ____________

  • 7. Visit the test server, then we can visit http://localhost:8080/ws/server?

  • 8. The server first lets it run, and then we create the client in the same project. This is relatively simple. You can prepare a cxf-client.xml file, configure the corresponding Web Service service interface, determine the access address, note that the HTTP address, Web Service is using HTTP protocol to communicate.
<?xml version="1.0" encoding="UTF-8"?>
<!--
        Licensed to the Apache Software Foundation (ASF) under one
        or more contributor license agreements. See the NOTICE file
        distributed with this work for additional information
        regarding copyright ownership. The ASF licenses this file
        to you under the Apache License, Version 2.0 (the
        "License"); you may not use this file except in compliance
        with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing,
        software distributed under the License is distributed on an
        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
        KIND, either express or implied. See the License for the
        specific language governing permissions and limitations
        under the License.
-->
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
    <bean id="client" class="com.github.cxf.server.CxfService" factory-bean="clientFactory" factory-method="create"/>
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.github.cxf.server.CxfService"/>
        <property name="address" value="http://localhost:8080/ws/server"/>
    </bean>
</beans>
        <!-- END SNIPPET: beans -->
  • 9. Then write a client startup program and run it. I think you will succeed! Because I saw the following picture!
package com.github.cxf.client;

import com.github.cxf.server.CxfService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Create by UncleCatMySelf in 21:56 2019\4\23 0023
 */
public class CxfClient {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:cxf-client.xml");
        CxfService client = (CxfService)context.getBean("client");
        System.out.println(client.say("MySelf"));
    }
}

Web Service is a cross-platform RPC technology protocol.

Public Number: Java Cat Says

Learning Communication Group: 728698035

Architecture design (code farmer) and entrepreneurship technical consultant, uninhibited mediocrity, love open source, chat about the process of life and irregular dry goods.

Posted by rledieu on Tue, 23 Apr 2019 09:09:33 -0700