webservice简单学习笔记
1. Web Service中的几个重要术语1.1 WSDL:web service definition language
1.2 SOAP:simple object access protocal
? 1.3 SEI:WebService EndPoint Interface(终端)
2、java调用免费webservice第一步:学会使用eclipse的webservice视图。 第二步:取得webservice的wsdl文档,即在访问路径后面加上?wsdl 第三步:得到wsdl文档后,在cmd命令中定位到项目的src目录,使用的jdk的命令“wsimport -keep wsdl的路径”生成java类。
? 第四步:根据wsdl文档结构调用代码 我们可以看见层级关系正好是类的调用过程。 【代码】 public static void main(String[] args) { // 这些调用代码由wsdl文档结构得来 WeatherWS ws = new WeatherWS(); WeatherWSSoap wwsoap = ws.getWeatherWSSoap(); List<String> aos = wwsoap.getWeather("大连",null).getString(); System.out.println(aos); } 3、使用CXF编写客户端??? 其实使用CXF开发client非常简单,和使用jdk是一样的区别在于要配置CXF的环境变量,之后生成的java类文件用指令:wsdl2java+ 路径,其他操作一样。 4、使用CXF编写服务端并发布??? 发布一个简单的webservice很简单,只需要以下几个类: 【接口】
/* * SEI: */ @WebService public interface HelloWS { @WebMethod public String sayHello(String name); } 【实现类】
/* * SEI的实现 */ @WebService public class HelloWSImpl implements HelloWS { @Override public String sayHello(String name) { System.out.println("serversayHello()"+name); return "Hello " +name; } } 【发布类】
/* * 发布Web Service */ public class ServerTest { public static void main(String[] args) { String address= "http://locahost:8989/day01_ws/hellows"; Endpoint.publish(address,new HelloWSImpl()); System.out.println("发布webservice成功!"); } } 5、CXF拦截器??? 拦截器一般有四种,客户端有入拦截器和出拦截器,服务端有入拦截器和出拦截器。拦截器只需要在客户端请求和服务端发布的时候使用,因此关键代码如下: 【客户端】 public static void main(String[] args) { HelloWSImplService factory = new HelloWSImplService(); HelloWS hellWS = factory.getHelloWSImplPort(); //发送请求的客户端对象 Client client = ClientProxy.getClient(hellWS); //客户端的日志出拦截器 List<Interceptor<? extends Message>>outInterceptors = client.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); //客户端的日志入拦截器 List<Interceptor<? extends Message>>inInterceptors = client.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor()); String result = hellWS.sayHello("BOB"); System.out.println("client"+result); } 【服务端】 public static void main(String[] args) { String address = "http://192.168.10.165:8888/day01_ws/datatypews"; Endpoint endpoint = Endpoint.publish(address,new HelloWSImpl()); System.out.println(endpoint); EndpointImpl endpointImpl = (EndpointImpl)endpoint; //服务端的日志入拦截器 List<Interceptor<? extends Message>>inInterceptors = endpointImpl.getInInterceptors(); inInterceptors.add(new LoggingInInterceptor()); //服务端的日志出拦截器 List<Interceptor<? extends Message>>outInterceptors = endpointImpl.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor()); System.out.println("发布webservice成功!"); } 6、CXF自定义拦截器??? 由上面拦截器可以看出,只需要new一个拦截器就可以应用了,因此自定义的拦截器如同系统拦截器一样使用。 【拦截器】
public class AddUserInterceptorextends AbstractPhaseInterceptor<SoapMessage> { private String name; private String password; public AddUserInterceptor(String name,String password) { super(Phase.PRE_PROTOCOL);// 准备协议化时拦截 this.name = name; this.password = password; } @SuppressWarnings("deprecation") @Override public void handleMessage(SoapMessage msg) throws Fault { List<Header> headers = msg.getHeaders(); /* * <atguigu><name>xfzhang</name> <password>123456</password></atguigu> */ Document document = DOMHelper.createDocument(); Element rootEle = document.createElement("atguigu"); Element nameELe = document.createElement("name"); nameELe.setTextContent(name); rootEle.appendChild(nameELe); Element passwordELe = document.createElement("password"); passwordELe.setTextContent(password); rootEle.appendChild(passwordELe); headers.add(new Header(new QName("atguigu"),rootEle)); System.out.println("client handleMessage()...."); } } 【调用】 调用和系统拦截器一样调用。 7、跨域问题??? 其实,用原生的ajax和jquery来请求webservice的时候是会出现跨域问题的,因此我们先请求本地的服务(如servlet),再通过HttpURLConnection发请求就不会出现跨域问题了,关键代码如下: 【关键代码】 protected void doPost(HttpServletRequestrequest,HttpServletResponse response) throws ServletException,IOException { Stringname = request.getParameter("name"); System.out.println("doPost"+name); Stringdata = "<soap:Envelopexmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><ns2:sayHelloxmlns:ns2=' URLurl = new URL("http://192.168.10.165:8888/day01_ws/datatypews"); HttpURLConnectionconnection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type","text/xml;charset=utf-8"); OutputStreamos = connection.getOutputStream(); os.write(data.getBytes("utf-8")); intresponseCode = connection.getResponseCode(); if(responseCode==200){ InputStreamis = connection.getInputStream();//String xml System.out.println("return"+is.available()); response.setContentType("text/xml;charset=utf-8"); ServletOutputStreamoutputStream = response.getOutputStream(); byte[]buffer = new byte[1024]; intlen = 0; while((len=is.read(buffer))>0){ outputStream.write(buffer,len); } outputStream.flush(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |