WebService简单介绍+代码
WebService是跨编程语言和操作平台的调用技术,
由XML+XSD, SOAP 和 WSDL 三大技术构成。
SOAP协议:http协议+xml数据格式
wsdl:基于xml语言,用于描述webservice及其函数,参数及其返回值。
wsdl文件保存在web服务器上,可以通过url地址访问,当客户端要调用一个webservice时,要知道xsdl地址。
发送流程大概未:wsdl->soap->date
适用场景:
1.跨防火墙通信
2.应用集成
3.B2B(跨公司开发)
4.软件和数据接口重用
java中使用webservice时候的几个注解
@WebService 服务名
@WebMethod 方法名
@RequestWrapper 请求参数包装
@ResponseWrapper 返回参数包装
@WebParam 请求参数
@WebEndpoint
调用wsdl代码:1.ElectronicsWebServiceSoap.java中导入webservice接口
2.实现ElectronicsWebService.java配置,包括@WebEndpoint,url等配置
3.Service层调用,将获取到response未string,我们将转换成json,querys需要和@requestwrapper相同,
4.
4.控制器使用
简单发布代码
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/*
*手机的业务类,该业务类通过webservice 对外提供服务
-
- 声明: @webservice
-
- 发布 EndPoint
*/
import javax.xml.ws.ResponseWrapper;
- 发布 EndPoint
@WebService (serviceName=“PhoneManager”,//修改服务名
targetNamespace=“http://dd.ws.it.cn”) //修改命名空间 ,默认包名,取反
//声明该业务类 对外提供webservice服务 ,默认只是对public 修饰的方法对外以webservice形式发布
public class PhoneService {
/*@WebMethod(operationName=“getMObileInfo”): 修改方法名
* @WebResult(name=“phone”):修改返回参数名
* @WebParam(name=“osName”):修改输入参数名
/
@WebMethod(operationName=“getMObileInfo”)
@WebResult(name=“phone”)
//@ResponseWrapper(localName = “Phone”, targetNamespace = “http://tempuri.org/”, className = “Phone”)
public Phone getPhoneInfo(@WebParam(name=“osName”)String osName){
Phone phone=new Phone();
if(osName.endsWith(“android”)){
phone.setName(“android”);phone.setOwner(“google”);phone.setTotal(80);
}else if(osName.endsWith(“ios”)){
phone.setName(“ios”);phone.setOwner(“apple”);phone.setTotal(15);
}else{
phone.setName(“windows phone”);phone.setOwner(“microsoft”);phone.setTotal(5);
}
return phone;
}
@WebMethod(exclude=true)//把该方法排除在外
public void sayHello(String city){
System.out.println(“你好:”+city);
}
private void sayLuck(String city){
System.out.println(“好友:”+city);
}
void sayGoodBye(String city){
System.out.println(“拜拜:”+city);
}
protected void saySayalala(String city){
System.out.println(“再见!”+city);
}
public static void main(String[] args) {
String address1=“http://127.0.0.1:8888/ws/phoneService”;
// String address2=“http://127.0.0.1:8888/ws/phoneManager”;
/
* 发布webservice服务
* 1.address:服务的地址
* 2:implementor 服务的实现对象
*/
Endpoint.publish(address1, new PhoneService());
// Endpoint.publish(address2, new PhoneService());
System.out.println(“wsdl地址 :”+address1+"?WSDL");
}
public static class Phone {
private String name;//操作系统名
private String owner;//拥有者
private int total;//市场占有率
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
}