本案例使用地址为: cxf入门
1.添加依赖
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
2.创建访问的接口HelloWorld以及实现类HelloWorldImpl
/**
* @description 创建定义的访问接口
* @author hy
* @date 2019-10-10
*/
@WebService
public interface HelloWorld {
String sayHi(String text);
String sayHiToUser(User user);
@XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
Map<Integer, User> getUsers();
}
@WebService(endpointInterface = "com.hy.cxf.start.demo.service.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
public String sayHiToUser(User user) {
System.out.println("sayHiToUser called");
users.put(users.size() + 1, user);
return "Hello " + user.getName();
}
public Map<Integer, User> getUsers() {
System.out.println("getUsers called");
return users;
}
}
3.创建User接口以及实现类
@XmlJavaTypeAdapter(UserAdapter.class)
public interface User {
String getName();
}
@XmlType(name = "User")
public class UserImpl implements User {
String name;
public UserImpl() {
}
public UserImpl(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.创建UserAdapter类
public class UserAdapter extends XmlAdapter<UserImpl, User> {
public UserImpl marshal(User v) throws Exception {
if (v instanceof UserImpl) {
return (UserImpl)v;
}
return new UserImpl(v.getName());
}
public User unmarshal(UserImpl v) throws Exception {
return v;
}
}
5.创建IntegerUserMap和IntegerUserMapAdapter
@XmlType(name = "IntegerUserMap")//最后被解析为这个类型
@XmlAccessorType(XmlAccessType.FIELD)
public class IntegerUserMap {
@XmlElement(nillable = false, name = "entry")
List<IntegerUserEntry> entries = new ArrayList<>();
public List<IntegerUserEntry> getEntries() {
return entries;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IdentifiedUser")
static class IntegerUserEntry {//这里是最终的类型
//Map keys cannot be null
@XmlElement(required = true, nillable = false)
Integer id;
User user;
public void setId(Integer k) {
id = k;
}
public Integer getId() {
return id;
}
public void setUser(User u) {
user = u;
}
public User getUser() {
return user;
}
}
}
/**
* @description 创建适配器
* @author hy
* @date 2019-10-10
*/
public class IntegerUserMapAdapter extends XmlAdapter<IntegerUserMap, Map<Integer, User>> {
public IntegerUserMap marshal(Map<Integer, User> v) throws Exception {
IntegerUserMap map = new IntegerUserMap();//当前的Map<Integer,User>最后会被解析为IntegerUserMap类型
for (Map.Entry<Integer, User> e : v.entrySet()) {
IntegerUserMap.IntegerUserEntry iue = new IntegerUserMap.IntegerUserEntry();
iue.setUser(e.getValue());
iue.setId(e.getKey());
map.getEntries().add(iue);
}
return map;
}
public Map<Integer, User> unmarshal(IntegerUserMap v) throws Exception {
Map<Integer, User> map = new LinkedHashMap<>();
for (IntegerUserMap.IntegerUserEntry e : v.getEntries()) {
map.put(e.getId(), e.getUser());
}
return map;
}
}
6.创建服务器Server类
/**
* @description 创建webService的服务器
* @author hy
* @date 2019-10-10
*/
public class Server {
protected Server() throws Exception {
HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class); //设置服务接口
svrFactory.setAddress("http://localhost:9000/helloWorld");
svrFactory.setServiceBean(implementor); //设置实际服务的Bean对象
svrFactory.create();
}
public static void main(String[] args) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
7.创建Client类
/**
* @description 创建客户端,用于访问当前的webservice
* @author hy
* @date 2019-10-10
*/
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:9000/helloWorld");
HelloWorld client = (HelloWorld) factory.create();//通过工厂创建一个对象
System.out.println(client.sayHi("World"));//调用webservice中的方法
User user = new UserImpl("World");
System.out.println(client.sayHiToUser(user));
// say hi to some more users to fill up the map a bit
user = new UserImpl("Galaxy");
System.out.println(client.sayHiToUser(user));
user = new UserImpl("Universe");
System.out.println(client.sayHiToUser(user));
System.out.println();
System.out.println("Users: ");
Map<Integer, User> users = client.getUsers();
for (Map.Entry<Integer, User> e : users.entrySet()) {
System.out.println(" " + e.getKey() + ": " + e.getValue().getName());
}
}
}
8.测试
启动Server类
1.通过web浏览器测试:http://localhost:9000/helloWorld?wsdl
2.使用Client访问
Client的结果
Server中的结果:
结果启动成功
9.总结
1.在服务器端提供的服务接口中一定要添加@WebService
,服务接口的实现类上需要添加@WebService(endpointInterface = "对应的接口的类全名", serviceName = "服务名")
2.Server端需要通过JaxWsServerFactoryBean创建一个服务
(需要指定当前的服务接口类和实际上调用的类型)
3.Client端需要JaxWsProxyFactoryBean创建一个可以调用的接口
,然后通过这个接口调用server端实际类的方法
4.如果需要对Map类型解析,需要创建 @XmlJavaTypeAdapter
(需要被适配的类型此类需要继承XmlAdapter)
以上纯属个人见解,如有问题请联系本人!