javascript
WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)
作者:@展云
本文為作者原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:https://www.cnblogs.com/zhanxiaoyun/p/7942902.html
目錄
一、CXF與Spring整合(jaxws:endpoint形式配置)1.新建一個(gè)maven項(xiàng)目
2.web.xml文件中配置CXFServlet以及Spring容器,配置Spring的配置文件,配置webservice的配置文件
二、客戶端調(diào)用(spring配置文件形式,不需要生成客戶端代碼)
1.spring配置文件信息
2.寫(xiě)對(duì)應(yīng)接口信息
3.加載Spring的配置文件進(jìn)行測(cè)試
4.需要的jar包
一、CXF與Spring整合(jaxws:endpoint形式配置)
工具要點(diǎn):idea、maven
1.新建一個(gè)maven項(xiàng)目
?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 自動(dòng)注冊(cè)bean,并檢查@Required,@Autowired的屬性已被注入 --><context:component-scan base-package="com.wp.learn.webservice"></context:component-scan><!-- 導(dǎo)入其他配置文件 --><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需要寫(xiě)完全路徑
情況二、Impl類注解是@Service,implementor只需“#”號(hào)加bean的id名
二、客戶端調(diào)用(spring配置文件形式,不需要生成客戶端代碼)
Webservice的客戶端調(diào)用,可以用ajax前端調(diào)用,java中可以通過(guò)生產(chǎn)客戶端代碼調(diào)用,也可以通過(guò)Spring配置文件、寫(xiě)對(duì)應(yīng)接口信息調(diào)用。本例介紹配置文件,寫(xiě)接口信息進(jìn)行webservice調(diào)用。
1.spring配置文件信息
<jaxws:client id="helloService" serviceClass="com.wp.learn.webservice.cxf.service.IHelloService"address="http://localhost:8080/ws/HelloService"> </jaxws:client>2.寫(xiě)對(duì)應(yīng)接口信息
?需要注意兩點(diǎn):
- 1、targetNamespace的值必須和webservice服務(wù)項(xiàng)目中定義的一致,具體信息可以在WSDL文件中查看
- 2、接口名稱可以自己隨便起,但是方法名稱、參數(shù)格式必須保持一致,否則無(wú)法找到服務(wù)的實(shí)現(xiàn)方法。
3.加載Spring的配置文件進(jìn)行測(cè)試
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
總結(jié)
以上是生活随笔為你收集整理的WebService--CXF与Spring的整合(jaxws:endpoint形式配置)以及客户端调用(spring配置文件形式,不需要生成客户端代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 阿里云服务器延迟多少?测一下
- 下一篇: JS十六进制转浮点、字符串转为Array