CXF开发webservice的小demo以及需要的jar包

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:46   3161   0

1.创建新的java项目即可 不需要web项目
下载需要的jar包 其中红色的jar是tomcat的jar包 在你的tomcat的lib文件夹下的 如果没有这个jar包运行时会报错
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletOutputStream


具体的类
接口类

import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;
@WebService
public interface UserServiceInter {


public User getReader(@WebParam(name="name") String name, @WebParam(name="password") String password);


public List<User> getReaders();


}


实现类

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;
@WebService(endpointInterface = "com.test.test.UserServiceInter",serviceName = "userService")
public class UserServiceImpl implements UserServiceInter{


public User getReader(String name, String password) {
return new User(name, password);
}


public List<User> getReaders() {
List<User> readerList = new ArrayList<User>();
User reader = new User("test01","123456");
User reader2 = new User("test02", "123456");
readerList.add(reader);
readerList.add(reader2);
return readerList;
}
}

测试类

import javax.xml.ws.Endpoint;
public class SimpleTest {
public static void main(String[] args) {
System.out.println("Server is starting");
UserServiceImpl userService = new UserServiceImpl();
Endpoint.publish("http://localhost:80/userService", userService);
System.out.println("Server is started");
}


}

登录测试

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class CallerTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(UserServiceInter.class);
factoryBean.setAddress("http://localhost:80/userService");


UserServiceInter readerService = (UserServiceInter)factoryBean.create();
User reader = readerService.getReader("test01","123456");
System.out.println("Reader:" + reader);
}
}


实体

public class User {


private String name;


private String password;


public User(){}


public User(String name , String password) {
this.name = name;
this.password = password;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String toString() {
return "name:" + name + ",password:" + password;
}


}



运行步骤
首先开启服务 运行SimpleTest


在运行CallerTest


浏览器运行一下连接 出现以下说明成功
http://localhost/userService?wsdl




demo以及jar的下载

链接: http://pan.baidu.com/s/1jGs9sZ8 密码: 2qqz









分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP