【Apache CXF】CXF对JAX-WS的支持
相关dependency,我使用的版本是2.7.11: <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> 以一个简单的Service为例: import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface MyCxfService { @WebMethod String saySth(String content); } 以及其实现: import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import pac.testcase.ws.MyCxfService; public class MyCxfServiceImpl implements MyCxfService { public String saySth(String content) { return "I say "+content; } } 启动服务: JaxWsServerFactoryBean server = new JaxWsServerFactoryBean(); server.setServiceClass(MyCxfServiceImpl.class); server.setAddress("http://localhost:8686/ws/service"); server.create(); 调用服务: JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean(); client.setServiceClass(MyCxfService.class); client.setAddress("http://localhost:8686/ws/service"); MyCxfService service = (MyCxfService)client.create(); System.out.println(service.saySth("nothing but performance!!")); CXF是通过Spring为service提供XML配置的。 <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
xmlns:jaxws="http://cxf.apache.org/jaxws" 和xsi:schemaLocation: http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 继续用上一个例子中的Service接口,简单做一下配置: <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"/> <jaxws:endpoint implementor="pac.king.webservice.impl.MyCxfServiceImpl" address="/MyCxfService" /> 访问:http://localhost:8080/runtrain/services/,会出现下面的效果: 客户端方面,可以使用jaxws:client配置让他调用本地bean那样简单: <jaxws:client id="MyCxfClient" address="http://localhost:8080/runtrain/services/MyCxfService" serviceClass="pac.king.webservice.MyCxfService" /> 如果不使用则相当于: <bean id="MyCxfClient" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" p:serviceClass="pac.king.webservice.MyCxfService" p:address="http://localhost:8080/runtrain/services/MyCxfService" /> 远程调用变得透明: ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext*.xml"); MyCxfService client = (MyCxfService)context.getBean("MyCxfClient"); System.out.println(client.saySth("nothing but show!!"));
这里引用一下wiki中XSD与JAXB对对照:
简单记录一下操作步骤。 * @param <BoundType> * The type that JAXB doesn't know how to handle. An adapter is written * to allow this type to be used as an in-memory representation through * the <tt>ValueType</tt>. * @param <ValueType> * The type that JAXB knows how to handle out of the box.
import java.util.HashMap; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import pac.king.pojo.User; import pac.king.webservice.utils.MyMapAdapter; @WebService public interface MyCxfService { @WebMethod @XmlJavaTypeAdapter(MyMapAdapter.class) public @WebResult HashMap<String,String> convertUserInfoToMap(@WebParam User user); } 以及实现: import java.util.HashMap; import pac.king.pojo.User; import pac.king.webservice.MyCxfService; public class MyCxfServiceImpl implements MyCxfService { public HashMap<String,String> convertUserInfoToMap(User user) { HashMap<String,String> result = new HashMap<String,String>(); result.put("name",user.getName()); result.put("id",user.getId()); result.put("password",user.getPassword()); return result; } }
package pac.king.pojo; public class User { private String id; private String name; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User() {} public User(String id,String name,String password) { super(); this.id = id; this.name = name; this.password = password; } } 注意服务方法上的注解@XmlJavaTypeAdapter(MyMapAdapter.class)。 import java.util.HashMap; import java.util.Map.Entry; import javax.xml.bind.annotation.adapters.XmlAdapter; public class MyMapAdapter extends XmlAdapter<MyGeneralBean[],HashMap<String,String>>{ @Override public HashMap<String,String> unmarshal(MyGeneralBean[] v) throws Exception { HashMap<String,String> resultMap = new HashMap<String,String>(); for (MyGeneralBean e : v) { resultMap.put(e.getKey(),e.getValue()); } return resultMap; } @Override public MyGeneralBean[] marshal(HashMap<String,String> v) throws Exception { MyGeneralBean[] m = new MyGeneralBean[10]; int i=0; for (Entry<String,String> entry : v.entrySet()) { m[++i] = new MyGeneralBean(entry.getKey(),entry.getValue()); } return m; } } MyGeneralBean是用来解释Map结构的一个简单类型,这个也需要提供一个无参数的constructor: public class MyGeneralBean { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public MyGeneralBean() {} public MyGeneralBean(String key,String value) { super(); this.key = key; this.value = value; } }
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext*.xml"); MyCxfService client = (MyCxfService) context.getBean("MyCxfClient"); Map<String,String> map = client.convertUserInfoToMap(new User("100001","King.","t;stmdtkg")); System.out.println(map.get("id")); System.out.println(map.get("name")); System.out.println(map.get("password")); 输出: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |