使用CXF实现Webservice的服务接口提供以及相关的客户端实现

至apache官网下载相关的jar包。

一。 编写提供服务的接口以及相关实现类

        

package com.demo;

import java.util.List;

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

import test.User;

@WebService
public interface HelloWorld {
	
	String sayHi(@WebParam(name = "text")String text);

	String sayHiToUser(User user);

	String[] SayHiToUserList(List<User> userList);
}

package com.demo;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import test.User;

@WebService(endpointInterface = "com.demo.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	Map<Integer, User> users = new LinkedHashMap<Integer, User>();

	public String sayHi(String text) {
		return "Hello " + text;
	}

	public String sayHiToUser(User user) {
		users.put(users.size() + 1, user);
		return "Hello " + user.getName();
	}

	public String[] SayHiToUserList(List<User> userList) {
		String[] result = new String[userList.size()];
		int i = 0;
		for (User u : userList) {
			result[i] = "Hello " + u.getName();
			i++;
		}
		return result;
	}

}

二。  无WEB容器,无Spring相关代码,仅通过JAVA项目代码模拟实现服务端接口提供以及客户端调用例子。

          1.  JAVA模拟服务端提供服务代码:

package test;

import javax.xml.ws.Endpoint;

import com.demo.HelloWorldImpl;

public class WebServiceAppServer {

	public static void main(String[] args) {

		System.out.println("web service start");
		HelloWorldImpl implementor = new HelloWorldImpl();
		String address = "http://localhost:8080/helloWorld";
		Endpoint.publish(address, implementor);
		System.out.println("web service started");

	}
}

           2.  运行如上代码,就可以模拟实现JAVA代码提供webservice服务,在浏览器中直接运行  http://localhost:8080/helloWorld?wsdl 即可发现服务已提供。

   3.  JAVA客户端代码实现调用服务接口:

package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.demo.HelloWorld;

public class HelloWorldNoSpringClient {

	public static void main(String[] args) {

		// TODO Auto-generated method stub
		JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
		svr.setServiceClass(HelloWorld.class);
		svr.setAddress("http://localhost:8080/helloWorld");
		HelloWorld hw = (HelloWorld) svr.create();
		User user = new User();
		user.setName("Tony");
		System.out.println(hw.sayHiToUser(user));
	}

}

三。  通过WEB容器,以及Spring实现服务的提供。以及客户端的代码调用例子。

          1.   在web.xml中配置相关信息,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			WEB-INF/classes/applicationContext.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>



	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<display-name>CXFServlet</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>

</web-app>

 2.  在src目录下,直接新增一个配置文件,如下:

<?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"
	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 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" />
        <!-- 提供服务端接口服务功能 -->
	<jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl" address="/helloWorld" />

	<bean id="client" class="com.demo.HelloWorld" factory-bean="clientFactory" factory-method="create" />

	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="com.demo.HelloWorld" />
		<property name="address" value="http://localhost:8080/CXF/webservice/helloWorld" />
	</bean>
</beans>

   3.  通过获取applicationContext.xml文件中的配置文件来获取相关BEAN,并调用服务端服务。

package test;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.demo.HelloWorld;

public class HelloWorldWithSpringClient {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorld client = (HelloWorld) context.getBean("client");
		User user1 = new User();
		user1.setName("Tony");
		User user2 = new User();
		user2.setName("freeman");
		List<User> userList = new ArrayList<User>();
		userList.add(user1);
		userList.add(user2);
		String[] res = client.SayHiToUserList(userList);
		System.out.println(res[0]);
		System.out.println(res[1]);

	}

}

  4.  也可以通过直接 组装获取参数来进行服务端接口调用。

		// TODO Auto-generated method stub
		JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
		svr.setServiceClass(HelloWorld.class);
		svr.setAddress("http://localhost:8080/helloWorld");
		HelloWorld hw = (HelloWorld) svr.create();
		User user = new User();
		user.setName("Tony");
		System.out.println(hw.sayHiToUser(user));


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值