赞
踩
<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"><soap:Header> <m:Trans xmlns:m="http://www.w3schools.com/transaction/" soap:mustUnderstand="1">234 </m:Trans></soap:Header><soap:Body> <m:GetPrice xmlns:m="http://www.w3schools.com/prices"> <m:Item>Apples</m:Item> </m:GetPrice></soap:Body></soap:Envelope>
package com.hsy.server; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; import com.hsy.pojo.User; @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text); String sayHiToUser(User user); String[] SayHiToUserList(List<User> userList); } package com.hsy.server; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.jws.WebParam; import javax.jws.WebService; import com.hsy.pojo.User; @WebService(endpointInterface="com.hsy.server.HelloWorld",serviceName="HelloWorld") public class HelloWorldImpl implements HelloWorld { Map<Integer, User> users = new LinkedHashMap<Integer, User>(); public String sayHi(@WebParam(name = "text") String text) { return "Hello,"+text; } public String sayHiToUser(User user) { users.put(users.size()+1, user); return "Hello,"+user.getName(); } public String[] SayHiToUserList(List<User> userList) { String[] result = new String[userList.size()]; int i = 0; for(User u:userList){ result[i] = "Hello " + u.getName(); i++; } return result; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } } package com.hsy.server; import javax.xml.ws.Endpoint; public class webServiceApp { /** * @param args */ public static void main(String[] args) { System.out.println("web service start"); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:8080/helloWorld"; Endpoint.publish(address, implementor); System.out.println("web service started"); } } package com.hsy.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.hsy.pojo.User; import com.hsy.server.HelloWorld; public class HelloWorldClient { /** * @param args */ public static void main(String[] args) { //首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码 JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean(); jwpfb.setServiceClass(HelloWorld.class); jwpfb.setAddress("http://localhost:8080/helloWorld"); HelloWorld hw = (HelloWorld) jwpfb.create(); User user = new User(); user.setName("name"); user.setDescription("toName"); System.out.println(hw.sayHiToUser(user)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。