CXF Realizes Remote Call

Keywords: Apache xml Spring SQL

Introduction to Apache CXF

Apache CXF = Celtix + Xfire

Holding multiple protocols
SOAP1.1,1.2
XML/HTTP
CORBA (Common Object Request Broker Architecture Common Object Request Broker Architecture, WS used in early languages). C,c++,C#)

And it can be quickly and seamlessly integrated with Spring

Flexible deployment: It can run on Tomcat, Jboss, Jetty (built-in), IBMWS,BeaWL.

Case Development

Server-side development

Step 1: Create a dynamic web project

Step 2: Import CXF-related jar packages

Step 3: Configure a Servlet provided by the CXF framework in web.xml

<!-- To configure CXF Provided by the framework Servlet -->
  <servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	<!-- Specify by initialization parameters CXF Framework configuration file location -->
  	<init-param>
  		<param-name>config-location</param-name>
  		<param-value>classpath:cxf.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>cxf</servlet-name>
  	<url-pattern>/service/*</url-pattern>
  </servlet-mapping>

Step 4: Provide cxf.xml under the classpath

<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://cxf.apache.org/bindings/soap 
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- Introduce CXF Bean The definition is as follows,Used in earlier versions -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

</beans>

Step 5: Develop an interface and implementation class

@WebService
public interface ICustomerService {
	public List<Customer> findAll();
}
@Transactional
public class CustomerServiceImpl implements ICustomerService {
        private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	@Transactional(readOnly=true)
	public List<Customer> findAll() {
		String sql = "select * from t_customer";
		List<Customer> list = jdbcTemplate.query(sql, new RowMapper<Customer>(){
			public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
				int id = rs.getInt("id");//Get the corresponding value from the result set according to the field name
				String name = rs.getString("name");
				String station = rs.getString("station");
				String telephone = rs.getString("telephone");
				String address = rs.getString("address");
				String decidedzone_id = rs.getString("decidedzone_id");
				return new Customer(id, name, station, telephone, address, decidedzone_id);
			}
		});
		return list;
	}
}

Step 6: Register services in cxf.xml

<bean id="customerService" class="com.naton.crm.service.CustomerServiceImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
	 
	<!-- Registration service -->
	<jaxws:server id="myService" address="/customer">
		<jaxws:serviceBean>
			<ref bean="customerService"/>
		</jaxws:serviceBean>
	</jaxws:server>


Client Development

Step 1: Create a Java project and import CXF related jar packages

Step 2: Generate native code using wsimport or CXF to provide the source code in 102java, only need to generate the interface file

Step 3: Copy the interface file into the project


Step 4: Provide spring configuration files to register client proxy objects

<!-- register crm Client proxy object -->
	<jaxws:client id="crmClient" 
		serviceClass="com.itheima.crm.ICustomerService" 
		address="http://localhost:8080/crm_naton/service/customer"/>

Step 5: Injecting proxy objects into Action by annotation


Sword casting team signature:

[Director] Twelve Spring and Autumn Periods, 3483099@qq.com

[Master] Ge Dao is not pale. han169@126.com

[Java Development] Rainbow, 343691194@qq.com Think of Qi Junhui. qiangzhang1227@163.com The little prince, 545106057@qq.com Patrol mountain drilling wind, 840260821@qq.com

[VS Development] Bean Points, 2268800211@qq.com

Asked the earth mirror. 847071279@qq.com Dust and freedom, 695187655@qq.com

[Big Data] Desert Oasis, caozhipan@126.com Zhang San Province, 570417591@qq.com

The Night Solitary Star, 11297761@qq.com

Three Stones, 261453882@qq.com Ordinary weird cakes, 591169003@qq.com

[Disaster Recovery Backup] Autumn Rain, 18568921@qq.com

Secret, you know.

Originator: Little Prince

Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Posted by jwoo on Sun, 19 May 2019 15:15:00 -0700