大家好,欢迎来到IT知识分享网。
首先写一个服务端:
package com.pp.ws.server; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService(targetNamespace="http://wsapi.pp1618.com", serviceName="PPLoginService") public class LoginService { @WebMethod public String login(@WebParam(name="username") String username, @WebParam(name="password") String password){ if(username != null && username.equals(password)){ return "OK"; } return "Faliure"; } } package com.pp.ws.server; import javax.xml.ws.Endpoint; public class AppServer { public static void main( String[] args ) { Endpoint.publish("http://127.0.0.1:6644/user/login", new LoginService()); } }
启动。 然后,用wsimport(JDK自带命令,本文章中使用的是JDK1.8)生成源代码,命令如下:
wsimport -s ./src -p com.pp.client.ws -encoding utf-8 http://127.0.0.1:6644/user/login?wsdl
另外新建一个maven项目,把上面生成的代码(src目录)拷贝到项目的src目录中去
代码结构如下:
并且加入spring依赖
调用的示例代码如下: package com.pp.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean; import com.pp.client.ws.LoginService; import com.pp.client.ws.PPLoginService; public class AppClient { //方式一 public static void fun1() throws Exception { URL url = new URL("http://127.0.0.1:6644/user/login?wsdl"); //QName的两个参数,参照LoginService上面的注解 QName qname = new QName("http://wsapi.pp1618.com", "PPLoginService"); Service service = Service.create(url, qname); LoginService ls = service.getPort(LoginService.class); System.out.println(ls.login("admin", "123")); System.out.println(ls.login("admin", "admin")); } //方式二(PPLoginService内部其实使用的是方式一) public static void fun2() throws Exception { URL url = new URL("http://127.0.0.1:6644/user/login?wsdl"); PPLoginService ppls = new PPLoginService(url); LoginService ls = ppls.getLoginServicePort(); System.out.println(ls.login("admin", "123")); System.out.println(ls.login("admin", "admin")); } //方式三,使用spring封装的方式 public static void fun3() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WSConfig.class); LoginService ls = context.getBean(LoginService.class); System.out.println(ls.login("admin", "123")); System.out.println(ls.login("admin", "admin")); context.close(); } public static void main(String[] args) throws Exception { fun1(); fun2(); fun3(); } } @Configuration class WSConfig { @Bean public JaxWsPortProxyFactoryBean createMainCouponService() throws MalformedURLException { //JaxWsPortProxyFactoryBean 类在spring-web.jar里面 JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean(); bean.setServiceInterface(LoginService.class); bean.setServiceName("PPLoginService"); bean.setWsdlDocumentUrl(new URL("http://127.0.0.1:6644/user/login?wsdl")); //设置超时 bean.addCustomProperty("com.sun.xml.internal.ws.request.timeout", 20000); bean.addCustomProperty("com.sun.xml.internal.ws.connect.timeout", 20000); return bean; } } org.springframework spring-web 4.3.2.RELEASE
需要注意的是:前两种方式是使用JDK自带的方式,第三种是spring封装的调用方式
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/46026.html