無缺格式見:https://www.zybuluo.com/Spancymath/note/1623555java
什麼是WebService?web
基於web的服務,使用Web(HTTP)方式,接收和響應外部系統的某種請求,從而實現遠程調用。ajax
//一、添加註解 @WebService public class OneService { //二、至少包含一個能夠對外公開的服務 public String sayHello(String name){ System.err.println("invoke "+name); return "Hello "+name; } public static void main(String[] args) { //三、第一個參數稱爲Binding即綁定地址, //第二個參數是實現者,即誰提供服務 Endpoint.publish("http://localhost:9999/one", new OneService()); } } //4.在IE地址欄輸入如下地址訪問說明文件: http://localhost:9999/one?wsdl
經過wsimport生成本地代碼spring
經常使用參數爲:
-d <目錄> - 將生成.class文件。默認參數。
-s <目錄> - 將生成.java文件。
-p <生成的新包名> -將生成的類,放於指定的包下。
(wsdlurl) - http://server:port/service?wsdl,必須的參數。
示例:
C:/> wsimport –s . http://192.168.0.100/one?wsdl apache
總結
WebService和Web服務器的區別:WebService是Web服務器的應用,Web服務器是WebService運行所必須的容器。
WebService其內部是經過Socket實現的。編程
WebService的特色:服務器
var xhr = new XMLHttpRequest(); function sedAjax() { var url = "http://localhost:8081/hello"; var nameText = document.getElementById("name").value; var request = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body><ns2:sayHi xmlns:ns2=\"http://server/\">" + "<arg0>" + nameText + "</arg0></ns2:sayHi></soap:Body></soap:Envelope>"; $.ajax({ url: url, type: "post", contentType: "application/soap+xml;charset=utf-8", data: request, success: function (data) { alert($(data).find("return").text()); } }) }
SOAP請求過程分析app
public class CxfServer1 implements ICxfServer1 { public String sayHi(String name) { return "Hello " + name; } public static void main(String[] args) { //聲明實例,使用ServerFactoryBean發佈服務 //使用CXF發佈一個服務,與JDK6發佈一個服務徹底不一樣 //* 即便是不使用@WebService註解,同樣能夠發佈成功 //* 即便此類沒有對外公佈的方法同樣能夠發佈成功 ServerFactoryBean bean = new ServerFactoryBean(); //綁定到發佈地址的端口 bean.setAddress("http://localhost:8080/cxf"); //設置服務接口,若是沒有接口,則爲本類 bean.setServiceClass(ICxfServer1.class); //設置接口實現類,即服務類 bean.setServiceBean(new CxfServer1()); //發佈服務 bean.create(); System.err.println("啓動成功"); } }
public class HelloImpl implements IHello { @Override public String sayHi(String name) { System.out.println("syaHi called"); return "Hello2 " + name; } public static void main(String[] args) { //使用jaxWs對其進行發佈 JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean(); //設置訪問地址 bean.setAddress("http://localhost:8081/hello"); //註冊服務接口 bean.setServiceClass(IHello.class); //註冊服務實現類 bean.setServiceBean(new HelloImpl()); //添加消息攔截器 bean.getInInterceptors().add(new LoggingInInterceptor()); bean.getOutInterceptors().add(new LoggingOutInterceptor()); //啓動 bean.create(); System.out.println("服務啓動完成..."); }
<servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <!--<init-param> <param-name>config-location</param-name> <param-value>classpath:cxf-servlet.xml</param-value> </init-param>--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
<?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:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF Bean定義以下,早期的版本中使用 --> <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 id="helloService" address="/hello" implementor="com.zhang.server1.HelloImpl"> <!--客戶端請求的消息攔截器--> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <!--服務端響應的消息攔截器--> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:endpoint>