本篇文章描述一个利用cxf框架开发webservice服务的demo,仅用于学习
环境准备:
自行下载cxf 3.0以上版本,并且在classpath和path中做相应配置
path : E:\apache-cxf-3.0.5\apache-cxf-3.0.5\bin
classpath : E:\apache-cxf-3.0.5\apache-cxf-3.0.5\lib\cxf-manifest.jar
环境测试:
cmd中运行wsdl2java
出现如图就成功:
服务端开发准备:
(项目目录)
具体代码:
UserDao.java
package com.pccw.dao;
import com.pccw.po.User;
public class UserDao {
public User getUser(String username){
User user = new User();
user.setUsername(username);
return user;
}
}
User.java
package com.pccw.po;
public class User {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "User [username=" + username + ", age=" + age + "]";
}
}
UserServer.java
package com.pccw.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.pccw.service.UserServiceInterface;
import com.pccw.service.UserServiceInterfaceImpl;
/**
* @author tl
* 发布服务
*/
public class UserServer {
public static void main(String[] args) {
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/userService");
jaxWsServerFactoryBean.setServiceClass(UserServiceInterface.class);
jaxWsServerFactoryBean.setServiceBean(new UserServiceInterfaceImpl());
jaxWsServerFactoryBean.create();
}
}
UserServerInterface.java
package com.pccw.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.pccw.po.User;
@WebService(targetNamespace = "http://service.pccw.com/",
serviceName = "UserService",
name = "UserService", portName = "UserService")
public interface UserServiceInterface {
@WebMethod(operationName="getUserByUserName",exclude=false)
public User getUserByUserName(@WebParam(name="username")String username) throws Exception;
@WebMethod(operationName="getAdd",exclude=false)
public String getAdd(@WebParam(name="username")String username,@WebParam(name="age")String age) throws Exception;
}
UserServiceInterfaceImpl .java
package com.pccw.service;
import com.pccw.dao.UserDao;
import com.pccw.po.User;
public class UserServiceInterfaceImpl implements UserServiceInterface{
private UserDao userDao = new UserDao();
public User getUserByUserName(String username) throws Exception {
return userDao.getUser(username);
}
public String getAdd(String username, String age) throws Exception {
return username + age;
}
}
以上代码会生成完成后在UserServer中发布服务,得到地址 http://127.0.0.1:12345/userService?wsdl ,接下来就可以进行客户端的开发
客户端开发:
在cmd 中执行 wsdl2java -d E:\WEB( 此处写自己的路径) http://127.0.0.1:12345/userService?wsdl
执行完成后再自己指定路径中将生成的文件夹放入项目中
java项目目录
其中的com.pccw.service就是wsdl2java命令生成的,具体代码就不展示了,只展示调用代码
TestMain.java
package com.pccw.client;
import com.pccw.service.User;
import com.pccw.service.UserServiceClient;
/**
* 客户端调用1
* 封装客户端调用
* @author tl
*/
public class TestMain {
public static void main(String[] args) throws Exception {
User user = UserServiceClient.openClient("tl");
System.out.println(user.getUsername());
String x = UserServiceClient.openClient2("tl", "-23");
System.out.println(x);
}
}
UserClient.java
package com.pccw.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.pccw.service.Exception_Exception;
import com.pccw.service.User;
import com.pccw.service.UserService;
/**
* 客户端调用2
* 使用cxf工厂调用
* @author tl
*
*/
public class UserClient {
public static void main(String[] args) throws Exception_Exception {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/userService");
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
UserService areaServiceSoap = (UserService) jaxWsProxyFactoryBean.create();
User user = areaServiceSoap.getUserByUserName("tl");
System.out.println(user.getUsername());
String add = areaServiceSoap.getAdd("tl", "23");
System.out.println(add);
}
}
UserServiceClient.java
package com.pccw.service;
import java.net.URL;
import javax.xml.namespace.QName;
import com.sun.istack.logging.Logger;
/**
* 客户端封装
* @author tl
*/
public class UserServiceClient {
private static final Logger LOG = Logger.getLogger(UserServiceClient.class);
private static final QName SERVICE_NAME = new QName("http://service.pccw.com/", "UserService");
private UserServiceClient() {
}
//method1
public static User openClient(String username) throws java.lang.Exception {
String url = "http://127.0.0.1:12345/userService?wsdl";
URL wsdlURL = new URL(url);
UserService_Service ss = new UserService_Service(wsdlURL, SERVICE_NAME);
UserService port = ss.getUserService();
User responseVo = port.getUserByUserName(username);
return responseVo;
}
//method2
public static String openClient2(String username,String age) throws java.lang.Exception {
String url = "http://127.0.0.1:12345/userService?wsdl";
URL wsdlURL = new URL(url);
UserService_Service ss = new UserService_Service(wsdlURL, SERVICE_NAME);
UserService port = ss.getUserService();
String responseVo2 = port.getAdd(username,age);
return responseVo2;
}
}
客户端的程序也就完成了,项目结束!
注意可能的错误: