基于CXF的webService本地数据交互----PC端(四)

 

不想新建项目了(可以参考前面一、二、三),就在现有的项目上说明吧,希望看得懂。

 

先配好服务器端。

 

1. 搭好Maven项目后,引入需要的包。pom.xml中增加:

<!-- CXF WebService -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf</artifactId>
			<version>2.4.6</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.neethi</groupId>
			<artifactId>neethi</artifactId>
			<version>3.0.1</version>
			<exclusions>
				<exclusion>
					<groupId>wsdl4j</groupId>
					<artifactId>wsdl4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.ws.xmlschema</groupId>
			<artifactId>xmlschema-core</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>com.sun.xml.bind</groupId>
			<artifactId>jaxb-xjc</artifactId>
			<version>2.2.6</version>
		</dependency>
		<dependency>
			<groupId>com.sun.xml.bind</groupId>
			<artifactId>jaxb-impl</artifactId>
			<version>2.2.6</version>
		</dependency>

 

2.web.xml中增加:

<!-- Apache CXFServlet -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>3</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webService/*</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.do</welcome-file>
	</welcome-file-list>

 

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		            <!-- cxf webservice Bean -->
		            /WEB-INF/webServiceBean.xml
		</param-value>
	</context-param>

 

3.webServiceBean.xml声明:

<?xml version="1.0" encoding="UTF-8"?>

	<!-- 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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">


	<!-- Import Apache CXF Bean Definition -->
	<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" />

	<!-- implementor :接口类的实现类 address: 要和注释里面神秘的服务名对应, -->
	<jaxws:endpoint id="synergyWebService"
		implementor="cn.iscas.ac.gz.synergy.system.webService.SynergyWebServiceImpl" address="/SynergyWebService" />

</beans>

 
基于CXF的webService本地数据交互----PC端(四)_第1张图片
 

4.SynergyWebService 接口定义:

package cn.iscas.ac.gz.synergy.system.application.webService;

import javax.jws.WebParam;
import javax.jws.WebService;

/** 
 * @Description webService接口
 *
 *
 */
@WebService
public interface SynergyWebService {

	/**
	 * 
	 * @Description 根据用户账号获取该用户信息
	 *
	 * @param account 用户账号
	 * @return
	 *
	 */
	public String getUser(@WebParam(name = "account") String account);
	
}

 

5.SynergyWebServiceImpl 接口实现:

package cn.iscas.ac.gz.synergy.system.webService;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import cn.iscas.ac.gz.synergy.system.application.webService.SynergyWebService;
import cn.iscas.ac.gz.synergy.system.domain.repository.UserRepository;

/** 
 * @Description webService接口实现  注意:@webservice自动注入一定要写
 *
 *注意这里的targetNamespace  一定是其实现接口的路径
 */
@WebService(endpointInterface = "cn.iscas.ac.gz.synergy.system.application.webService.SynergyWebService", serviceName = "SynergyWebService",targetNamespace="http://webService.application.system.synergy.gz.ac.iscas.cn/")
public class SynergyWebServiceImpl implements SynergyWebService {
	
	/*
	 * 用户管理
	 */
	@Autowired
	private UserRepository userInfoRepo;
	

	@Override
	public String getUser(String account) {
            //逻辑自己写
		User user = userInfoRepo.getUserInfoByUserName(account);
		List<User> users = new ArrayList<User>();
		users.add(user);
		String result = this.buildJSONForUsers(users);
		return result;
	}
}

 

OK,如果没什么问题,服务端算是搭建OK了,访问下地址是否有XML文档:

http://127.0.0.1:8080/项目名/webService/SynergyWebService?wsdl


基于CXF的webService本地数据交互----PC端(四)_第2张图片
 

 

OK,接下来写客户端,动态调用:

package com.test;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @Description
 * 
 * @author yeyuelong Email:[email protected]
 * @version 1.0 创建时间 2013-2-26
 * 
 */
public class CXFClient {

	public static void main(String[] args) {
		try {
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
			org.apache.cxf.endpoint.Client client = dcf.createClient("http://127.0.0.1:8080/Synergy/webService/SynergyWebService?wsdl");

			Object[] objects = client.invoke("getUser","admin@gziscas");
			// 输出调用结果
			System.out.println(objects[0].toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

运行下,看是否可以获取到数据:



基于CXF的webService本地数据交互----PC端(四)_第3张图片
 
 

相关参考:

 

http://wenku.baidu.com/view/1343f96a1eb91a37f1115c0d.html

 

http://lost-alien.iteye.com/blog/1175859

 

你可能感兴趣的:(wsdl,CXF,pc)