1.使用jdk发布webservice java
import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpoint; /** * * @author lili */ @WebService public class TestWebService { /** 供客户端调用方法 该方法是非静态的,会被发布 * @param name 传入参数 * @return 返回结果 * */ public String getValue(String name){ System.out.println("okokokoko: "+name); return "hello "+name; }; /** * 方法上加@WebMentod(exclude=true)后,此方法不被发布; * @param name * @return */ @WebMethod(exclude=true) public String getHello(String name){ return "你好! "+name; } /** 静态方法不会被发布 * @param name * @return */ public static String getString(String name){ return "再见!"+name; } //经过EndPoint(端点服务)发布一个WebService public static void main(String[] args) { /*参数:1,本地的服务地址; 2,提供服务的类; */ Endpoint.publish("http://192.168.1.4:8081/Service/ServiceHello", new TestWebService()); System.out.println("发布成功!"); //发布成功后 在浏览器输入 http://192.168.1.4:8081/Service/ServiceHello?wsdl } }
2. java远程访问 webserviceweb
import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public static void main(String[] args) { try { String endpoint = URL; // 直接引用远程的wsdl文件 // 如下都是套路 Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName(new QName("http://testwebservice.mycompany.com/","Hello"));// WSDL里面描述的接口名称 call.addParameter("arg0", XMLType.XSD_DATE,ParameterMode.IN);// 接口的参数 “使用arg0” 服务端才能接收到数据 call.setReturnType(XMLType.XSD_STRING);// 设置返回类型 String temp = "ddd"; //访问数据 String result = (String) call.invoke(new Object[]{temp}); // 给方法传递参数,而且调用方法 System.out.println("result is " + result); } catch (Exception e) { System.err.println(e.toString()); } return ""; }