Web Service (四) 手动发布Web Service接口-CXF与Spring集成

╰半夏微凉° 2022-08-14 02:52 146阅读 0赞
  1. 当我们发布完Web Service接口之后有两种方式可以调用Web service服务,一种是通过动态客户端方式,另一种是引用服务端的接口,引用服务端接口的方式对于客户端同服务器端耦合比较大,而使用WSDL的方式客户端不知道服务端的存在就可以调用服务器的方法。
  2. 下面是项目的结构图:
  3. 1.Web Service发布项目

![Image 1][]

2.编写服务端接口类以及实现类,如下,同上一篇自动发布接口,多了一个注解@WebService

  1. package com.test.webservice;
  2. import javax.jws.WebService;
  3. @WebService
  4. public interface IHelloWorld {
  5. public String sayHello(User user);
  6. public String sayHello1();
  7. }
  8. package com.test.webservice;
  9. import javax.jws.WebMethod;
  10. import javax.jws.WebService;
  11. @WebService(endpointInterface="com.test.webservice.IHelloWorld",serviceName="helloService123")
  12. public class HelloWorldImpl implements IHelloWorld {
  13. @Override
  14. public String sayHello(User user) {
  15. System.out.println("开始调用Web Service method:sayHello()");
  16. return user.getUsername()+"lilongsheng";
  17. }
  18. @Override
  19. @WebMethod
  20. public String sayHello1() {
  21. System.out.println("开始调用Web Service method:sayHello1()");
  22. return "lilongsheng1";
  23. }
  24. }
  25. 在实现类上面还可以加上SOAP注解,如
  26. @SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
  27. 表示Web Service方法传递数据的类型、编码等设置。

参考:http://dlc-cdn.sun.com/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html/zh\_CN/api/javax/jws/soap/SOAPBinding.html

3.Spring配置文件配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  8. <import resource="classpath:META-INF/cxf/cxf.xml" />
  9. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  10. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  11. <jaxws:endpoint id="helloService456" implementor="com.test.webservice.HelloWorldImpl" address="/helloService789"/>
  12. </beans>

4.客户端测试代码

  1. package com.test.webservice;
  2. //import org.apache.cxf.endpoint.Client;
  3. import org.apache.cxf.endpoint.Client;
  4. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
  5. public class testClient {
  6. public static void main(String[] args) {
  7. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
  8. //创建客户端
  9. Client client = dcf.createClient("http://192.168.24.82:8080/Web_Service_Spring/ws/helloService789?wsdl");
  10. Object[] objects;
  11. try {
  12. User user=new User();
  13. user.setUsername("longsheng");
  14. objects = client.invoke("sayHello",user);
  15. //输出调用结果
  16. System.out.println(objects[0].toString());
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }
  22. 上面传参为传实体,实体需要实现序列化接口才能先通过网络传输。Web service在系统间以及系统交互上用的很广泛。

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,146人围观)

还没有评论,来说两句吧...

相关阅读