WebService服务端开发

2017-10-23  本文已影响0人  藝龍

Java WebService服务端开发


JWS

  1. 介绍: JWS-WebService.JWS是Java语言实现的一种webservice,用来开发和发布服务,它是一个轻量级的WS框架,实现起来也非常的简单
  2. 实例:
package org.zttc.service;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface IMyService {  
    @WebResult(name="addResult")  
    public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);  
    @WebResult(name="loginUser")  
    public User login(@WebParam(name="username")String username,@WebParam(name="password")String password);  
}
package org.zttc.service;  
  
import javax.jws.WebParam;  
import javax.jws.WebResult;  
import javax.jws.WebService;  
  
@WebService(endpointInterface="org.zttc.service.IMyService")  
public class MyServiceImpl implements IMyService {  
  
    @Override  
    public int add(int a, int b) {  
        System.out.println(a + "+" + b + "=" + (a + b));  
        return a+b;  
    }  
  
    @Override  
    public User login(@WebParam(name = "username") String username,  
            @WebParam(name = "password") String password) {  
        System.out.println(username + " is logging");  
        User user = new User();  
        user.setId("1");  
        user.setUsername(username);  
        user.setPassword(password);  
        return user;  
          
    }  
}  
package org.zttc.service;  
  
import javax.xml.ws.Endpoint;  
  
public class MyServer {  
      
    public static void main(String[] args){  
        String address = "http://localhost:8888/ns";  
        Endpoint.publish(address, new MyServiceImpl());  
    }  
      
}  
package org.zttc.service;  
  
import java.net.MalformedURLException;  
import java.net.URL;  
  
import javax.xml.namespace.QName;  
import javax.xml.ws.Service;  
  
public class TestClient {  
  
    public static void main(String[] args){  
         try {  
             //创建访问wsdl服务地址的url  
            URL url = new URL("http://localhost:8888/ns?wsdl");  
            //通过QName指明服务的和具体信息  
             QName sname= new QName("http://service.zttc.org/","MyServiceImplService");  
             //创建服务  
             Service service = Service.create(url,sname);  
             //实现接口  
             IMyService ms =service.getPort(IMyService.class);  
             System.out.println(ms.add(12, 33));  
        } catch (MalformedURLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
    }  
}  

Axis2

  1. 官网:http://axis.apache.org/axis2/java/core/

CXF Apache CXF = Celtix + XFire

  1. 官网: http://cxf.apache.org/
  1. XFire在2007年后已停止更新。正式更名为Apache CXF,亦可以说是XFire2.0

Java WebService客户端开发


使用JDK命令生成客户端调用代码
wsimport -p com.xxx -keep http://10.1.108.155/Services/MobileRegisterService.svc?wsdl URI地址必须要以wsdl结尾

客户端调用代码:(MobileRegisterService,IMobileRegisterService主要使用这个类和接口)

MobileRegisterService mrs = new MobileRegisterService();
IMobileRegisterService imrs = webServicesSoap.getBasicHttpBindingIMobileRegisterService();
imrs.method();

使用axis2中bin目录下的wsdl2java.bat生成

wsdl2java -uri http://10.1.108.155/Services/MobileRegisterService.svc?wsdl -o D:\ -p com.xxx

上一篇下一篇

猜你喜欢

热点阅读