原生的WebService的實(shí)現(xiàn)方式
1汗洒、創(chuàng)建一個(gè)jar工程
2议纯、定義一個(gè)我們要發(fā)布的接口
package haiyang.yu.webservice;
import javax.jws.WebService;
/**
* Created on 2018-04-16 8:31
* <p>Title: haiyang.yu.webservice</p>
* <p>Description: </p>
*
* @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
* @version 1.0
*/
@WebService
public interface TimeAlarmBell {
/**
* 問(wèn)候當(dāng)前時(shí)間
* @param name your name
* @return results
*/
String giveMeDate(String name);
}
3、實(shí)現(xiàn)接口將并實(shí)現(xiàn)相應(yīng)的業(yè)務(wù)
package haiyang.yu.webservice;
import javax.jws.WebService;
import java.util.Date;
/**
* Created on 2018-04-16 8:33
* <p>Title: haiyang.yu.webservice</p>
* <p>Description: </p>
*
* @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
* @version 1.0
*/
@WebService(endpointInterface= "haiyang.yu.webservice.TimeAlarmBell",serviceName="showDate")//指定webservice所實(shí)現(xiàn)的接口以及服務(wù)名稱(chēng)
public class TimeAlarmBellImpl implements TimeAlarmBell {
@Override
public String giveMeDate(String name) {
return name.concat(",您好溢谤!現(xiàn)在是北京時(shí)間:").concat(new Date().toString());
}
}
4瞻凤、發(fā)布webservice
package haiyang.yu.webservice;
import javax.xml.ws.Endpoint;
/**
* Created on 2018-04-16 8:36
* <p>Title: haiyang.yu.webservice</p>
* <p>Description: </p>
*
* @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
* @version 1.0
*/
public class TimeAlarmBellPublish {
public static void main(String[] args) {
TimeAlarmBell showDate = new TimeAlarmBellImpl();
//調(diào)用Endpoint的publish方法發(fā)布Web Service
Endpoint.publish("http://192.168.10.104:8085/showDate", showDate);
System.out.println("Web Service發(fā)布成功憨攒!");
}
}
5、webservice的調(diào)用
package haiyang.yu.webservice;
//import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created on 2018-04-16 8:48
* <p>Title: haiyang.yu.webservice</p>
* <p>Description: </p>
*
* @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
* @version 1.0
*/
public class TimeAlarmBellConnection {
public static void main(String[] args) {
// ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(
// 1, new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(false).build());
// scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
// @Override
// public void run() {
//創(chuàng)建WSDL的URL阀参,注意不是服務(wù)地址
URL url = null;
try {
url = new URL("http://192.168.10.104:8085/showDate?wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
//創(chuàng)建服務(wù)名稱(chēng)
//1.namespaceURI - 命名空間地址
//2.localPart - 服務(wù)視圖名
QName qname = new QName("http://webservice.yu.haiyang/", "showDate");
//創(chuàng)建服務(wù)視圖
//參數(shù)解釋?zhuān)? //1.wsdlDocumentLocation - wsdl地址
//2.serviceName - 服務(wù)名稱(chēng)
Service service = Service.create(url, qname);
//獲取服務(wù)實(shí)現(xiàn)類(lèi)
TimeAlarmBell showDate = service.getPort(TimeAlarmBell.class);
//調(diào)用查詢方法
String result = showDate.giveMeDate("KevinBruce");
System.out.println(result);
// }
// }, 1, 2, TimeUnit.SECONDS);
}
}
注釋掉的內(nèi)容中使用定時(shí)任務(wù)去循環(huán)調(diào)用webservice的接口了肝集,如果想要使用這個(gè)定時(shí)任務(wù),需要引入common-lang3的jar包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
使用axis1.4的實(shí)現(xiàn)方式
下載地址:http://archive.apache.org/dist/ws/axis/1_4/
下載axis-bin-1_4.zip 這個(gè)包
使用axis1實(shí)現(xiàn)webservice在構(gòu)建時(shí)可能會(huì)相對(duì)麻煩一些蛛壳,但是構(gòu)建成功后杏瞻,對(duì)于一些webservice的方法的實(shí)現(xiàn)就簡(jiǎn)單了很多,可以大大的提高開(kāi)發(fā)效率衙荐。
1捞挥、創(chuàng)建一個(gè)war工程(這是必須的,axis1使用war來(lái)發(fā)布接口)
2忧吟、創(chuàng)建包并定義一個(gè)接口(正常的接口砌函,不需要注解)
package haiyang.yu.axis1;
/**
* Created on 2018-04-16 16:37
* <p>Title: haiyang.yu.axis1</p>
* <p>Description: </p>
*
* @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
* @version 1.0
*/
public interface Hello {
/**
* 獲取當(dāng)前時(shí)間
* @param user 獲取用戶
* @return Result
*/
String getLocalDate(String user);
}
3、給這個(gè)接口定義一個(gè)實(shí)現(xiàn)類(lèi)(該實(shí)現(xiàn)類(lèi)需要有@WebService注解)
package haiyang.yu.axis1;
import javax.jws.WebService;
import java.util.Date;
/**
* Created on 2018-04-16 16:38
* <p>Title: haiyang.yu.axis1</p>
* <p>Description: </p>
*
* @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
* @version 1.0
*/
@WebService
public class HelloImpl implements Hello {
@Override
public String getLocalDate(String user) {
return user.concat(",您好溜族! 當(dāng)前時(shí)間為:").concat(new Date().toString());
}
}
當(dāng)你寫(xiě)完實(shí)現(xiàn)類(lèi)的時(shí)候讹俊,你的webservice的接口就已經(jīng)實(shí)現(xiàn)了。接下來(lái)的任務(wù)就是配置了斩祭。
4劣像、配置要發(fā)布為webservice的方法
- 修改wabapps/WEB-INF/web.xml文件(添加一個(gè)servlet映射)。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
- 創(chuàng)建一個(gè)server-config.wsdd文件(在文件中添加以下內(nèi)容)
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration>
<parameter name="sendMultiRefs" value="true" />
<parameter name="disablePrettyXML" value="true" />
<parameter name="adminPassword" value="admin" />
<parameter name="dotNetSoapEncFix" value="true" />
<parameter name="enableNamespacePrefixOptimization" value="false" />
<parameter name="sendXMLDeclaration" value="true" />
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" />
<parameter name="sendXsiTypes" value="true" />
<requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session" />
</handler>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request" />
<parameter name="extension" value=".jwr" />
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" />
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
<transport name="http">
<requestFlow>
<handler type="URLMapper" />
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
</requestFlow>
<parameter name="qs:list"
value="org.apache.axis.transport.http.QSListHandler" />
<parameter name="qs:wsdl"
value="org.apache.axis.transport.http.QSWSDLHandler" />
<parameter name="qs.list"
value="org.apache.axis.transport.http.QSListHandler" />
<parameter name="qs.method"
value="org.apache.axis.transport.http.QSMethodHandler" />
<parameter name="qs:method"
value="org.apache.axis.transport.http.QSMethodHandler" />
<parameter name="qs.wsdl"
value="org.apache.axis.transport.http.QSWSDLHandler" />
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder" />
</responseFlow>
</transport>
<!-- 想要添加或者取消修改一下內(nèi)容即可摧玫,AdminService與Version注銷(xiāo)不提供服務(wù)。 -->
<service name="SayHello" provider="java:RPC">
<parameter name="allowedMethods" value="*" />
<parameter name="className" value="haiyang.yu.axis1.HelloImpl" />
<namespace>http://www.yuhaiyang.com/ocean-axis/sayHello</namespace>
</service>
</deployment>
5诬像、將工程打成war 包放到j(luò)etty或者是Tomcat的webapps下,同時(shí)將axis-bin-1_4.zip包中解壓出來(lái)的文件中的webapps目錄下的內(nèi)容復(fù)制到j(luò)etty或者是Tomcat的webapps下闸婴。
6坏挠、啟動(dòng)容器我們定義的url即可。
使用axis2-1.7.7的實(shí)現(xiàn)方式
下載地址:http://axis.apache.org/axis2/java/core/download.html
下載 axis2-1.7.7-war.zip 這個(gè)包
沒(méi)使用過(guò)邪乍,后續(xù)更新