web service1简单的例子用jdk自带的类
1,建立自己的java项目my_service
2,建立包,com.hjg.service
3,创建类:
IMyService.java
package com.hjg.service; import javax.jws.WebService; @WebService public interface IMyService { int add(int a, int b); int minus(int a,int b); }
加上@Webservice注解
JAX-WS注解,表示java api xml for webservice。JDK自带API的XML格式的webservice
MyServiceImpl.java
package com.hjg.service; import javax.jws.WebService; @WebService(endpointInterface="com.hjg.service.IMyService") public class MyServiceImp implements IMyService { @Override public int add(int a, int b) { System.out.println(a+"+"+b+" = "+(a+b)); return (a+b); } @Override public int minus(int a, int b) { System.out.println(a+"*"+b+" = "+(a*b)); return (a*b); } }
endpointInterface指定接入点接口:接口必须存在
服务器端,MyService.java
package com.hjg.service; import javax.xml.ws.Endpoint; public class MyServer { public static void main(String[] args) { //访问方式:http://localhost:8888/ns?wsdl String address = "http://localhost:8888/ns"; Endpoint.publish(address, new MyServiceImp()); } }
客户端,MyClient.java
package com.hjg.service; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class MyClient { public static void main(String[] args) { try { //服务WSDL Document的地址 URL url = new URL("http://localhost:8888/ns"); //Qnameqname是qualified name 的简写 //构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成 //由发布的wsdl可知namespace为http://service.hjg.com/, QName sname = new QName("http://service.hjg.com/","MyServiceImpService"); Service service = Service.create(url, sname); IMyService ms = service.getPort(IMyService.class); int rs = ms.add(1, 2); System.out.println(rs); } catch (Exception e) { e.printStackTrace(); } } }
浏览器访问http:localhost:8888/ns?wsdl 返回:
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> - <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.hjg.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.hjg.com/" name="MyServiceImpService"> - <types> - <xsd:schema> <xsd:import namespace="http://service.hjg.com/" schemaLocation="http://localhost:8888/ns?xsd=1" /> </xsd:schema> </types> - <message name="minus"> <part name="parameters" element="tns:minus" /> </message> - <message name="minusResponse"> <part name="parameters" element="tns:minusResponse" /> </message> - <message name="add"> <part name="parameters" element="tns:add" /> </message> - <message name="addResponse"> <part name="parameters" element="tns:addResponse" /> </message> - <portType name="IMyService"> - <operation name="minus"> <input wsam:Action="http://service.hjg.com/IMyService/minusRequest" message="tns:minus" /> <output wsam:Action="http://service.hjg.com/IMyService/minusResponse" message="tns:minusResponse" /> </operation> - <operation name="add"> <input wsam:Action="http://service.hjg.com/IMyService/addRequest" message="tns:add" /> <output wsam:Action="http://service.hjg.com/IMyService/addResponse" message="tns:addResponse" /> </operation> </portType> - <binding name="MyServiceImpPortBinding" type="tns:IMyService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="minus"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> - <operation name="add"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> </binding> - <service name="MyServiceImpService"> - <port name="MyServiceImpPort" binding="tns:MyServiceImpPortBinding"> <soap:address location="http://localhost:8888/ns" /> </port> </service> </definitions>