WebService学习更新中~
1.Jax-ws:java api ?xml webservice ?入门?SEI:Service Endpoint Interface ? ?服务提供的接口——ICalcService SIB:Service Implemetion Bean ? ?实现服务的具体类——CalcServiceImpl 下面是利用jdk1.6.0_38的环境,实现简单的webService。 步骤: 服务器:
1.创建服务接口(ICalcService) 2.创建服务实现类(CalcServiceImpl) 3.创建服务发布类 (Server) 客户端:客户端调用远程接口,访问服务。 服务器短代码: package com.techbirds.service; import javax.jws.WebService; @WebService public interface ICalcService { public int add(int a,int b); public int del(int a,int b); } package com.techbirds.service; import javax.jws.WebService; @WebService(endpointInterface="com.techbirds.service.ICalcService") public class CalcServiceImpl implements ICalcService { /* (non-Javadoc) * @see com.techbirds.service.IOneService#add(int,int) */ @Override public int add(int a,int b) { System.out.println("a+b="+(a+b)); return a+b; } /* (non-Javadoc) * @see com.techbirds.service.IOneService#del(int,int) */ @Override public int del(int a,int b) { System.out.println("a-b="+(a-b)); return a-b; } } package com.techbirds.service; import javax.xml.ws.Endpoint; /** * @author techbirds * */ public class Server { /** * @param args */ public static void main(String[] args) { String address="http://localhost:8888/ns"; Endpoint.publish(address,new CalcServiceImpl()); } } 客户端代码: package com.techbirds.service; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class Client { public static void main(String[] args) { try { URL url=new URL("http://localhost:8888/ns?wsdl"); QName sname=new QName("http://service.techbirds.com/","CalcServiceImplService"); Service service=Service.create(url,sname); ICalcService ics=service.getPort(ICalcService.class); ics.add(1,2); ics.del(2,1); //System.out.println(ics.add(1,2)); //System.out.println(ics.del(2,1)); } catch (MalformedURLException e) { e.printStackTrace(); } } } 项目结构: 此demo的缺点在于:ICalcSercie接口必须提供给客户端,否则其不知如何使用远程服务。后续中会有改善。。 ps:在使用jdk自带实现webservice时,会遇到com.sun.xml.internal.ws.model.RuntimeModelerException异常,原因在于jdk版本不够高不支持,为此我直接下载了jdk1.6.0_38 ? 。下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |