您的位置:首页 > 其它

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

2013-04-28 15:18 931 查看
至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));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐