1、服务端代码:
package net.ilkj.soap.server; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * 服务接口,用注解指明 */ @WebService public interface IHelloService { @WebMethod(operationName="selectMaxAge_method") //发布的XML中的方法名 @WebResult(name="c")//返回值 public Customer selectMaxAgeStudent(@WebParam(name="c1")Customer c1,@WebParam(name="c2")Customer c2);//参数名 @WebMethod(operationName="selectMaxLongName_method") @WebResult(name="c") public Customer selectMaxLongNameStudent(@WebParam(name="c1")Customer c1,@WebParam(name="c2")Customer c2); @WebMethod(operationName="my_method") public void myMethod(); // @WebMethod(exclude=true) 不把此方法发布公开服务 @WebMethod(operationName="exclude_method") public void excludeMethod(); }
2、实现类:
package net.ilkj.soap.server; import javax.jws.WebService; //如果实现类实现了多个接口时就需要使用下面的方式指定哪个接口是SEI,公开服务 @WebService(endpointInterface="net.ilkj.soap.server.IHelloService", serviceName="IHelloService", portName="IHelloServicePort") public class HelloServiceImpl implements IHelloService { @Override public Customer selectMaxAgeStudent(Customer c1, Customer c2) { Customer c=new Customer(); boolean bool=c1.getBirthday().getTime()>c2.getBirthday().getTime(); c=bool?c2:c1; return c; } @Override public Customer selectMaxLongNameStudent(Customer c1, Customer c2) { Customer c=new Customer(); boolean bool=c1.getName().length()>c2.getName().length(); c=bool?c1:c2; return c; } @Override public void excludeMethod() { System.out.println("这是一个不被公开的方法"); } @Override public void myMethod() { System.out.println("哈哈哈"); } }
3、使用到的实体类:
package net.ilkj.soap.server; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; //SOAP消息格式封装的是XML代码,所以在发送消息时需要将JAVA Object转换为 //XML,下面的注解就是将Customer转换为公开WEB服务的接口中的参数类型和返回值 @XmlRootElement(name="Customer") public class Customer { private long id; private String name; private Date birthday; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
4、发布服务类:
package net.ilkj.soap.server; import javax.xml.ws.Endpoint; /** * 发布服务 * @author Administrator * */ public class SoapServer { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/helloService", new HelloServiceImpl()); } }
5、客户端调用——1
package net.ilkj.soap.server; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; /** * 使用Axis2调用 * @author Administrator * */ public class Client_1 { public static void main(String[] args) throws MalformedURLException { Service service = Service.create(new URL("http://localhost:8080/helloService?wsdl"), new QName("http://server.soap.ilkj.net/","IHelloService")); IHelloService iHelloService=service.getPort(IHelloService.class); Customer c1=new Customer(); c1.setId(1); c1.setName("ABC"); Customer c2=new Customer(); c2.setId(2); c2.setName("BCDEFF"); Customer c=iHelloService.selectMaxLongNameStudent(c1, c2); System.out.println(c.getName()); } }
6、客户端调用——2
package net.ilkj.soap.server; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class Client_2 { /** * @param args * @throws ParseException * 使用CXF调用 */ public static void main(String[] args) throws ParseException { JaxWsProxyFactoryBean soapFactoryBean=new JaxWsProxyFactoryBean(); soapFactoryBean.setAddress("http://localhost:8080/helloService"); soapFactoryBean.setServiceClass(IHelloService.class); Object o=soapFactoryBean.create(); IHelloService helloService=(IHelloService) o; Customer c1=new Customer(); c1.setId(1); c1.setName("A"); GregorianCalendar calendar=(GregorianCalendar) GregorianCalendar.getInstance(); calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1989-01-28")); Customer c2=new Customer(); c2.setId(2); c2.setName("B"); calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1990-01-28")); // c2.setBirthday(new XMLGregorianCalendarImpl(calendar)); System.out.println(helloService.selectMaxAgeStudent(c1, c2).getName()); } }
7、客户端调用——3
package net.ilkj.soap.server; import java.rmi.RemoteException; import net.ilkj.soap.server.IHelloServiceStub.SelectMaxLongNameStudent; import net.ilkj.soap.server.IHelloServiceStub.SelectMaxLongNameStudentResponse; import org.apache.axis2.AxisFault; /** * 使用工具生成的客户端类来调用 * @author Administrator * */ public class Client_3 { public static void main(String[] args) { try { //实例化一个客户端类对象 IHelloServiceStub iHelloServiceStub=new IHelloServiceStub(); //实例化一个内部类,这里在stub客户端类中,原来的方法都已经被封装成了内部类,比如方法selectMaxLongNameStudent() SelectMaxLongNameStudent selectMaxLongNameStudent=new SelectMaxLongNameStudent(); //这里也是一个内部类,是实体,作为方法的参数selectMaxLongNameStudent(Customer,Customer) IHelloServiceStub.Customer c1=new IHelloServiceStub.Customer(); c1.setName("aaaaa"); IHelloServiceStub.Customer c2=new IHelloServiceStub.Customer(); c2.setName("bb"); selectMaxLongNameStudent.setC1(c1); selectMaxLongNameStudent.setC2(c2); SelectMaxLongNameStudentResponse response=iHelloServiceStub.selectMaxLongNameStudent(selectMaxLongNameStudent); IHelloServiceStub.Customer c=response.get_return(); System.out.println(c.getName()); } catch (AxisFault e) { e.printStackTrace(); }catch (RemoteException e1) { e1.printStackTrace(); } } }
8、客户端调用——4
package net.ilkj.soap.server; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; /** * 使用RPC方式调用 * @author Administrator * */ public class Client_4 { public static void main(String[] args) { try { RPCServiceClient rpcClient=new RPCServiceClient(); Options options=rpcClient.getOptions(); EndpointReference endReference=new EndpointReference("http://localhost:8080/helloService?wsdl"); options.setTo(endReference); QName aName=new javax.xml.namespace.QName("http://server.soap.ilkj.net", "selectMaxAgeStudent"); Customer c1=new Customer(); c1.setId(1); c1.setName("ABC"); Customer c2=new Customer(); c2.setId(2); c2.setName("BCDEFF"); Object[] objects=rpcClient.invokeBlocking(aName, new Object[]{c1,c2}, new Class[]{Customer.class}); System.out.println(objects[0]); } catch (AxisFault e) { // TODO Auto-generated catch block } } }