学习WebService笔记
目前开发WebServic主要使用的技术有以下几个,JAX-WS、Axis、XFire、CXF。因为使用Spring的关系吧,所以偏好于后面两者,XFire和CXF。
WebServic服务端
使用XFire和CXF,和Spring的集成WebServic真的是非常简单。
XFire的使用
XFire发布服务有两种方式,jsr181和直接配置式,个人比较喜欢标注的东西,所以也选择当然选择了jsr181方式发布服务,其实两者都是很快能上手的。
Web.xml
<servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
public interface IHello {public String getName( String name);}
package net.company.webservicimport javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebService(name = "Hello", serviceName = "Hello", targetNamespace = "http://company.net")public class Hello implements IHello {@WebMethod@WebResult(name = "resultMsg")public String getName(@WebParam(name = "name") String name) {return "Hello: " + name;}}
<service><serviceClass>net.company.webservic.Hello</serviceClass><serviceFactory>jsr181</serviceFactory></service>
package net.company.springCXF;import java.util.List;import javax.jws.WebParam;import javax.jws.WebService;/** * @author KennyLee * */@WebServicepublic interface IContactUsService { List<Message> getMessages(); void postMessage(@WebParam(name = "message") Message message); String sayHello(@WebParam(name = "name") String name);}
package net.company.springCXF;import java.util.ArrayList;import java.util.List;import javax.jws.WebService;/** * @author KennyLee * */@WebService(endpointInterface = "net.company.springCXF.IContactUsService")public final class ContactUsServiceImpl implements IContactUsService {/* * (non-Javadoc) * * @see net.company.springCXF.IContactUsService#getMessages() */public List<Message> getMessages() {List<Message> messages = new ArrayList<Message>();messages.add(new Message("LiLei");messages.add(new Message("HanMeimei");return messages;}/* * (non-Javadoc) * * @see * net.company.springCXF.IContactUsService#postMessage(net.company.springCXF * .Message) */public void postMessage(Message message) {System.out.println(message);}public String sayHello(String name) {return "Hello: " + name;}}
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>Spring with CXF</display-name><description>CXF demo</description><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <jsp-config><jsp-property-group><url-pattern>*.jsp</url-pattern><el-ignored>false</el-ignored></jsp-property-group></jsp-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
<?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:context="http://www.springframework.org/schema/context"xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"default-autowire="byName"><!-- Load CXF modules from cxf.jar --><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" /><!-- Enable message logging using the CXF logging feature --><cxf:bus><cxf:features><cxf:logging /></cxf:features></cxf:bus><!-- The service bean --><bean id="contactUsServiceImpl" /><!-- Aegis data binding --><bean id="aegisBean" /><bean id="jaxws-and-aegis-service-factory" ref="aegisBean" /><property name="serviceConfigurations"><list><bean /><bean /><bean /></list></property></bean><!-- Service endpoint --><!--See http://incubator.apache.org/cxf/faq.html regarding CXF + SpringAOP--><jaxws:endpoint id="contactUsService"implementoraddress="/contactus"><jaxws:serviceFactory><ref bean="jaxws-and-aegis-service-factory" /></jaxws:serviceFactory></jaxws:endpoint></beans>