快捷搜索:  汽车  科技

在spring中service标注什么(几种调用webservice的方式)

在spring中service标注什么(几种调用webservice的方式)代码结构如下:并且加入spring依赖

首先写一个服务端:


  1. package com.pp.ws.server;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5. @WebService(targetNamespace="http://wsapi.pp1618.com" serviceName="PPLoginService")
  6. public class LoginService {
  7. @WebMethod
  8. public String login(@WebParam(name="username") String username @WebParam(name="password") String password){
  9. if(username != null && username.equals(password)){
  10. return "OK";
  11. }
  12. return "Faliure";
  13. }
  14. }
  15. package com.pp.ws.server;
  16. import javax.xml.ws.Endpoint;
  17. public class AppServer {
  18. public static void main( String[] args ) {
  19. Endpoint.publish("http://127.0.0.1:6644/user/login" new LoginService());
  20. }
  21. }

启动。然后,用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中service标注什么(几种调用webservice的方式)(1)

并且加入spring依赖


  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-web</artifactId>
  4. <version>4.3.2.RELEASE</version>
  5. </dependency>
  6. 调用的示例代码如下:
  7. package com.pp.client;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import javax.xml.namespace.QName;
  11. import javax.xml.ws.Service;
  12. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Configuration;
  15. import org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean;
  16. import com.pp.client.ws.LoginService;
  17. import com.pp.client.ws.PPLoginService;
  18. public class AppClient {
  19. //方式一
  20. public static void fun1() throws Exception {
  21. URL url = new URL("http://127.0.0.1:6644/user/login?wsdl");
  22. //QName的两个参数,参照LoginService上面的注解
  23. QName qname = new QName("http://wsapi.pp1618.com" "PPLoginService");
  24. Service service = Service.create(url qname);
  25. LoginService ls = service.getPort(LoginService.class);
  26. System.out.println(ls.login("admin" "123"));
  27. System.out.println(ls.login("admin" "admin"));
  28. }
  29. //方式二(PPLoginService内部其实使用的是方式一)
  30. public static void fun2() throws Exception {
  31. URL url = new URL("http://127.0.0.1:6644/user/login?wsdl");
  32. PPLoginService ppls = new PPLoginService(url);
  33. LoginService ls = ppls.getLoginServicePort();
  34. System.out.println(ls.login("admin" "123"));
  35. System.out.println(ls.login("admin" "admin"));
  36. }
  37. //方式三,使用spring封装的方式
  38. public static void fun3() throws Exception {
  39. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(WSConfig.class);
  40. LoginService ls = context.getBean(LoginService.class);
  41. System.out.println(ls.login("admin" "123"));
  42. System.out.println(ls.login("admin" "admin"));
  43. context.close();
  44. }
  45. public static void main(String[] args) throws Exception {
  46. fun1();
  47. fun2();
  48. fun3();
  49. }
  50. }
  51. @Configuration
  52. class WSConfig {
  53. @Bean
  54. public JaxWsPortProxyFactoryBean createMainCouponService() throws MalformedURLException {
  55. //JaxWsPortProxyFactoryBean 类在spring-web.jar里面
  56. JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean();
  57. bean.setServiceInterface(LoginService.class);
  58. bean.setServiceName("PPLoginService");
  59. bean.setWsdlDocumentUrl(new URL("http://127.0.0.1:6644/user/login?wsdl"));
  60. //设置超时
  61. bean.addCustomProperty("com.sun.xml.internal.ws.request.timeout" 20000);
  62. bean.addCustomProperty("com.sun.xml.internal.ws.connect.timeout" 20000);
  63. return bean;
  64. }
  65. }

猜您喜欢: