对于webservice,我个人用CXF比较多,所以下面写个文章教教入门,啰嗦话不多说
先写个对象
package com.shadow.extras.cxf; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @XmlAccessorType(XmlAccessType.FIELD) @SuppressWarnings("serial") public class User implements Serializable { public Integer id; private String username; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
然后写服务端接口
package com.shadow.extras.cxf; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface UserWebService { public User queryUser(@WebParam(name = "user") User user, @WebParam(name = "id") Integer id); public Integer queryCount(@WebParam(name = "count") Integer count); }
接着实现我们刚刚写的接口
package com.shadow.extras.cxf; import javax.jws.WebService; @WebService(endpointInterface = "com.shadow.extras.cxf.UserWebService") public class UserWebServiceImpl implements UserWebService { @Override public User queryUser(User user, Integer id) { System.out.println("当前用户名: " + user.getUsername()); System.out.println("id参数: " + id); return user; } @Override public Integer queryCount(Integer count) { return count; } }
上面的步骤完成后就开始配置我们的xml
<?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" xmlns:cxf="http://cxf.apache.org/core" 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"> <!-- ***注意*** 手动添加的内容: xmlns:jaxws="http://cxf.apache.org/jaxws" 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" /> <bean id="userWebService" class="com.shadow.extras.cxf.UserWebServiceImpl" /> <jaxws:endpoint id="queryService" implementor="#userWebService" address="/query" /> </beans>
最后就写个main测试效果
package com.shadow.extras.cxf; import java.lang.reflect.Method; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class CXFClient { public static void main(String[] args) { JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); Client client = factory.createClient("http://localhost/services/query?wsdl"); Object user = null; try { user = Thread.currentThread().getContextClassLoader().loadClass("com.shadow.extras.cxf.User").newInstance(); // 获取参数对象的实例 Method method = user.getClass().getMethod("setUsername", String.class); // 通过方法设置对象的属性值 method.invoke(user, "ssss"); Object[] result = client.invoke("queryUser", user, 50); // 按照方法的参数来提供值 if(result != null && result.length > 0){ System.out.println(result[0].getClass().getMethod("getUsername").invoke(result[0])); // 通过getUsername来获取对象的username属性 } Object[] result2 = client.invoke("queryCount", 101); if(result2 != null && result2.length > 0){ System.out.println(result2[0]); // 基本类型返回就直接输出,或者强转对应类型 } } catch (Exception e) { e.printStackTrace(); } } }