當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)
生活随笔
收集整理的這篇文章主要介紹了
WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者:@展云
本文為作者原創,轉載請注明出處:https://www.cnblogs.com/zhanxiaoyun/p/7942902.html
目錄
一、CXF與Spring整合(jaxws:endpoint形式配置)1.新建一個maven項目
2.web.xml文件中配置CXFServlet以及Spring容器,配置Spring的配置文件,配置webservice的配置文件
二、客戶端調用(spring配置文件形式,不需要生成客戶端代碼)
1.spring配置文件信息
2.寫對應接口信息
3.加載Spring的配置文件進行測試
4.需要的jar包
一、CXF與Spring整合(jaxws:endpoint形式配置)
工具要點:idea、maven
1.新建一個maven項目
?pom.xml
2.web.xml文件中配置CXFServlet以及Spring容器,配置Spring的配置文件,配置webservice的配置文件
web.xml:
?web.xml
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><!-- 使用annotation 自動注冊bean,并檢查@Required,@Autowired的屬性已被注入 --><context:component-scan base-package="com.wp.learn.webservice"></context:component-scan><!-- 導入其他配置文件 --><import resource="applicationContext-ws.xml" /></beans>applicationContext-ws.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="helloWebservice" implementor="#helloServiceImpl"address="/hello" /> </beans>接口HelloService:
Impl類HelloServiceImpl:
情況一、如果Impl類的注解是@Webservice,implementor需要寫完全路徑
情況二、Impl類注解是@Service,implementor只需“#”號加bean的id名
二、客戶端調用(spring配置文件形式,不需要生成客戶端代碼)
Webservice的客戶端調用,可以用ajax前端調用,java中可以通過生產客戶端代碼調用,也可以通過Spring配置文件、寫對應接口信息調用。本例介紹配置文件,寫接口信息進行webservice調用。
1.spring配置文件信息
<jaxws:client id="helloService" serviceClass="com.wp.learn.webservice.cxf.service.IHelloService"address="http://localhost:8080/ws/HelloService"> </jaxws:client>2.寫對應接口信息
?需要注意兩點:
- 1、targetNamespace的值必須和webservice服務項目中定義的一致,具體信息可以在WSDL文件中查看
- 2、接口名稱可以自己隨便起,但是方法名稱、參數格式必須保持一致,否則無法找到服務的實現方法。
3.加載Spring的配置文件進行測試
public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-ws.xml");IHelloService helloService = (IHelloService) context.getBean("helloService");String result = helloService.sayHi("帥哥");System.out.print(result);} }4.需要的jar包
?pom.xml
?源碼地址:https://gitee.com/wangpinggs/learn/tree/master/webserviceDemo
總結
以上是生活随笔為你收集整理的WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云服务器延迟多少?测一下
- 下一篇: JS十六进制转浮点、字符串转为Array