您的位置:首页 > 其它

【WebService】通过CXF发布WebService第一例

2014-07-14 10:49 316 查看
CXF的理论知识就不重复了。

发布服务

1、在OSGI bundle中发布,所以首先创建OSGI工程,并加入CXF所需要的库文件,

MANIFEST.MF文件内容:

[plain]
view plaincopyprint?

Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: MySimpleWebServiceCXF01 Bundle-SymbolicName: MySimpleWebServiceCXF01 Bundle-Version: 1.0.0.qualifier Bundle-Activator: mysimplewebservicecxf01.Activator Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Import-Package: org.apache.commons.logging;version="1.0.4", org.osgi.framework;version="1.3.0" Bundle-ClassPath: lib/geronimo-activation_1.1_spec-1.1.jar, lib/geronimo-annotation_1.0_spec-1.1.1.jar, lib/geronimo-javamail_1.4_spec-1.7.1.jar, lib/geronimo-jaxws_2.2_spec-1.1.jar, lib/geronimo-servlet_2.5_spec-1.1.2.jar, lib/geronimo-stax-api_1.0_spec-1.0.1.jar, lib/geronimo-ws-metadata_2.0_spec-1.1.3.jar, lib/jaxb-api-2.2.3.jar, lib/jaxb-impl-2.2.4-1.jar, lib/jetty-util-7.5.4.v20111024.jar, lib/neethi-3.0.1.jar, lib/saaj-api-1.3.4.jar, lib/saaj-impl-1.3.12.jar, lib/wsdl4j-1.6.2.jar, lib/xml-resolver-1.2.jar, lib/xmlschema-core-2.0.1.jar, ., lib/cxf-2.5.2.jar, lib/org.eclipse.equinox.http.jetty_2.0.0.v20100503.jar, lib/jetty-server-7.5.4.v20111024.jar, lib/jetty-http-7.5.4.v20111024.jar, lib/jetty-continuation-7.5.4.v20111024.jar, lib/jetty-io-7.5.4.v20111024.jar, lib/jetty-security-7.5.4.v20111024.jar, lib/stax2-api-3.1.1.jar, lib/wstx-asl-3.2.0.jar

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: MySimpleWebServiceCXF01
Bundle-SymbolicName: MySimpleWebServiceCXF01
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: mysimplewebservicecxf01.Activator
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.apache.commons.logging;version="1.0.4",
org.osgi.framework;version="1.3.0"
Bundle-ClassPath: lib/geronimo-activation_1.1_spec-1.1.jar,
lib/geronimo-annotation_1.0_spec-1.1.1.jar,
lib/geronimo-javamail_1.4_spec-1.7.1.jar,
lib/geronimo-jaxws_2.2_spec-1.1.jar,
lib/geronimo-servlet_2.5_spec-1.1.2.jar,
lib/geronimo-stax-api_1.0_spec-1.0.1.jar,
lib/geronimo-ws-metadata_2.0_spec-1.1.3.jar,
lib/jaxb-api-2.2.3.jar,
lib/jaxb-impl-2.2.4-1.jar,
lib/jetty-util-7.5.4.v20111024.jar,
lib/neethi-3.0.1.jar,
lib/saaj-api-1.3.4.jar,
lib/saaj-impl-1.3.12.jar,
lib/wsdl4j-1.6.2.jar,
lib/xml-resolver-1.2.jar,
lib/xmlschema-core-2.0.1.jar,
.,
lib/cxf-2.5.2.jar,
lib/org.eclipse.equinox.http.jetty_2.0.0.v20100503.jar,
lib/jetty-server-7.5.4.v20111024.jar,
lib/jetty-http-7.5.4.v20111024.jar,
lib/jetty-continuation-7.5.4.v20111024.jar,
lib/jetty-io-7.5.4.v20111024.jar,
lib/jetty-security-7.5.4.v20111024.jar,
lib/stax2-api-3.1.1.jar,
lib/wstx-asl-3.2.0.jar


2、声明一个WebService服务接口并实现

WebService接口文件 IUserManager.java

[java]
view plaincopyprint?

package mysimplewebservicecxf01.user.api; import java.util.List; import javax.jws.WebService; import mysimplewebservicecxf01.user.api.model.User; @WebService public interface IUserManager { int addUser(String name); List<User> queryUsers(); User queryUser(int userId); boolean deleteUser(int userId); }

package mysimplewebservicecxf01.user.api;

import java.util.List;

import javax.jws.WebService;

import mysimplewebservicecxf01.user.api.model.User;

@WebService
public interface IUserManager {
int addUser(String name);
List<User> queryUsers();
User queryUser(int userId);
boolean deleteUser(int userId);
}
Java Bean对象User.java

[java]
view plaincopyprint?

package mysimplewebservicecxf01.user.api.model; public class User { public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; private String name; }

package mysimplewebservicecxf01.user.api.model;

public class User {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}


WebService的实现 UserManager.java

[java]
view plaincopyprint?

package mysimplewebservicecxf01.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mysimplewebservicecxf01.user.api.IUserManager;
import mysimplewebservicecxf01.user.api.model.User;

public class UserManager implements IUserManager {
Map<Integer, User> userMap = new HashMap<Integer, User>();
int newID = 0;
@Override
public int addUser(String name) {

int newId = this.newID++;
System.out.println("add a user begin,userId:"+newId + ",name:"+name);
User user = new User();
user.setId(newId);
user.setName(name);
userMap.put(newId, user);
System.out.println("add a user ok");
return newId;
}

@Override
public List<User> queryUsers() {
System.out.println("query all users begin");
List<User> userList = new ArrayList<User>();
for(User user:userMap.values()){
userList.add(user);
}
System.out.println("query all users end,count:"+userList.size());
return userList;
}

@Override
public User queryUser(int userId) {
System.out.println("query a user begin, userId:"+userId);
if(userMap.containsKey(userId)){
User user = userMap.get(userId);
System.out.println("query a user begin, userId:"+userId+", name:"+user.getName());
return user;
}
System.out.println("query a user end,no the user("+userId+")");
return null;
}

@Override
public boolean deleteUser(int userId) {
System.out.println("delete a user begin, userId:"+userId);
if(userMap.containsKey(userId)){
User user = userMap.remove(userId);
System.out.println("delete a user end, userId:"+userId+", name:"+user.getName());
}
return true;
}
}

package mysimplewebservicecxf01.user.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mysimplewebservicecxf01.user.api.IUserManager;
import mysimplewebservicecxf01.user.api.model.User;

public class UserManager implements IUserManager {
Map<Integer, User> userMap = new HashMap<Integer, User>();
int newID = 0;
@Override
public int addUser(String name) {

int newId = this.newID++;
System.out.println("add a user begin,userId:"+newId + ",name:"+name);
User user = new User();
user.setId(newId);
user.setName(name);
userMap.put(newId, user);
System.out.println("add a user ok");
return newId;
}

@Override
public List<User> queryUsers() {
System.out.println("query all users begin");
List<User> userList = new ArrayList<User>();
for(User user:userMap.values()){
userList.add(user);
}
System.out.println("query all users end,count:"+userList.size());
return userList;
}

@Override
public User queryUser(int userId) {
System.out.println("query a user begin, userId:"+userId);
if(userMap.containsKey(userId)){
User user = userMap.get(userId);
System.out.println("query a user begin, userId:"+userId+", name:"+user.getName());
return user;
}
System.out.println("query a user end,no the user("+userId+")");
return null;
}

@Override
public boolean deleteUser(int userId) {
System.out.println("delete a user begin, userId:"+userId);
if(userMap.containsKey(userId)){
User user = userMap.remove(userId);
System.out.println("delete a user end, userId:"+userId+", name:"+user.getName());
}
return true;
}
}


3、实现一个WebService服务发布的工具类,用来发布WebService服务

[java]
view plaincopyprint?

package mysimplewebservicecxf01.publish;

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

import mysimplewebservicecxf01.service.interceptor.SampleInterceptorService;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class ServiceManager {
private static final ServiceManager instance = new ServiceManager();
List<String> serviceList = new ArrayList<String>();

public static ServiceManager getInstance(){
return instance;
}

private ServiceManager(){

}

public void publish(Class<?> clazz, Object implObj){
//创建WebService服务工厂
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//注册WebService接口
factory.setServiceClass(clazz);
//发布接口
factory.setAddress("http://localhost:9000/" +clazz.getSimpleName());
factory.setServiceBean(implObj);
// factory.getInInterceptors().add(new LoggingInInterceptor());
// factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.getInInterceptors().add(new SampleInterceptorService());
//创建WebService
factory.create();
}
}

package mysimplewebservicecxf01.publish;

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

import mysimplewebservicecxf01.service.interceptor.SampleInterceptorService;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class ServiceManager {
private static final ServiceManager instance = new ServiceManager();
List<String> serviceList = new ArrayList<String>();

public static ServiceManager getInstance(){
return instance;
}

private ServiceManager(){

}

public void publish(Class<?> clazz, Object implObj){
//创建WebService服务工厂
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//注册WebService接口
factory.setServiceClass(clazz);
//发布接口
factory.setAddress("http://localhost:9000/" +clazz.getSimpleName());
factory.setServiceBean(implObj);
//        factory.getInInterceptors().add(new LoggingInInterceptor());
//        factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.getInInterceptors().add(new SampleInterceptorService());
//创建WebService
factory.create();
}
}


4、最后在Activator的start函数中,使用ServiceManager 将服务发布出来

[java]
view plaincopyprint?

package mysimplewebservicecxf01; import mysimplewebservicecxf01.publish.ServiceManager; import mysimplewebservicecxf01.user.api.IUserManager; import mysimplewebservicecxf01.user.impl.UserManager; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { private static BundleContext context; public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; ServiceManager.getInstance().publish(IUserManager.class, new UserManager()); } public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } }

package mysimplewebservicecxf01;

import mysimplewebservicecxf01.publish.ServiceManager;
import mysimplewebservicecxf01.user.api.IUserManager;
import mysimplewebservicecxf01.user.impl.UserManager;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

private static BundleContext context;

public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;

ServiceManager.getInstance().publish(IUserManager.class, new UserManager());

}

public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}

}


客户端调用服务器

[java]
view plaincopyprint?

package mysimplewebservicecxf01;

import java.util.List;

import mysimplewebservicecxf01.client.interceptor.SampleInterceptorClient;
import mysimplewebservicecxf01.user.api.IUserManager;
import mysimplewebservicecxf01.user.api.model.User;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {

public static void main(String[] args) {

IUserManager userMgr = (IUserManager) getService(IUserManager.class);
List<User> userList = userMgr.queryUsers();
int count = 0;
/*还不清楚为什么,当返回的List为空时,客户端收到的却是null*/
if(null != userList){
count = userList.size();

}
System.out.println("Count:"+count);

int userId01 = userMgr.addUser("User01");

userList = userMgr.queryUsers();
count = userList.size();
System.out.println("Count:"+count);

int userId02 = userMgr.addUser("User02");

userList = userMgr.queryUsers();
count = userList.size();
System.out.println("Count:"+count);

userMgr.deleteUser(userId02);

userList = userMgr.queryUsers();
count = userList.size();
System.out.println("Count:"+count);
}

public static Object getService(Class<?> clazz){
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(clazz);
//设置WebService地址
factory.setAddress("http://localhost:9000/"+clazz.getSimpleName());
factory.getOutInterceptors().add(new SampleInterceptorClient());
return factory.create();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: