Spring中使用CXF
关键字: cxf spring webservice
在Spring中采用CXF来使用WebService是很方便的,这是按照Apache官方网站上的文章写的。
1.Web服务接口HelloWorld.java:
Java代码
1. package demo.spring;
2.
3. import javax.jws.WebService;
4.
5. @WebService
6. public interface HelloWorld {
7. String sayHi(String text);
8. }
2.实现类HelloWorldImpl.java:
Java代码
1. package demo.spring;
2.
3. import javax.jws.WebService;
4.
5. @WebService(endpointInterface = "demo.spring.HelloWorld")
6. public class HelloWorldImpl implements HelloWorld {
7.
8. public String sayHi(String text) {
9. return "Hello " + text;
10. }
11. }
3.Spring配置文件beans.xml:
Xml代码
1. <beans xmlns="http://www.springframework.org/schema/beans"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xmlns:jaxws="http://cxf.apache.org/jaxws"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans
5. http://www.springframework.org/schema/beans/spring-beans.xsd
6. http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
7.
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.
12. <jaxws:endpoint
13. id="helloWorld"
14. implementor="demo.spring.HelloWorldImpl"
15. address="/HelloWorld" />
16.
17. </beans>
4.在web.xml文件中加入:
Xml代码
1. <context-param>
2. <param-name>contextConfigLocation</param-name>
3. <param-value>WEB-INF/beans.xml</param-value>
4. </context-param>
5.
6. <listener>
7. <listener-class>
8. org.springframework.web.context.ContextLoaderListener
9. </listener-class>
10. </listener>
11.
12. <servlet>
13. <servlet-name>CXFServlet</servlet-name>
14. <servlet-class>
15. org.apache.cxf.transport.servlet.CXFServlet
16. </servlet-class>
17. <load-on-startup>1</load-on-startup>
18. </servlet>
19.
20. <servlet-mapping>
21. <servlet-name>CXFServlet</servlet-name>
22. <url-pattern>/*</url-pattern>
23. </servlet-mapping>
5.客户端调用时Client.java:
Java代码
1. package demo.spring.client;
2.
3. import demo.spring.HelloWorld;
4.
5. import org.springframework.context.support.ClassPathXmlApplicationContext;
6.
7.
8. public final class Client {
9.
10. private Client() {
11. }
12.
13. public static void main(String args[]) throws Exception {
14. ClassPathXmlApplicationContext context
15. = new ClassPathXmlApplicationContext(new String[] {"demo/spring/client/client-beans.xml"});
16.
17. HelloWorld client = (HelloWorld)context.getBean("helloWorld");
18.
19. String response = client.sayHi("Joe");
20. System.out.println("Response: " + response);
21. }
22. }
client-beans.xml
Xml代码
1. <beans xmlns="http://www.springframework.org/schema/beans"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xmlns:jaxws="http://cxf.apache.org/jaxws"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans
5. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
6. http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
7.
8. <bean id="helloWorld" class="demo.spring.HelloWorld"
9. factory-bean="clientFactory" factory-method="create"/>
10.
11. <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
12. <property name="serviceClass" value="demo.spring.HelloWorld"/>
13. <property name="address" value="http://localhost:8080/cxf2/HelloWorld"/>
14. </bean>
15.
16. </beans>
分享到:
相关推荐
本java工程实现了spring+cxf快速开发webservice jar包齐全导入myeclipse可直接二次开发
java语言实现使用spring+mybatis+oracle访问数据库,使用cxf发布webservice,并使用axis调用webservice。项目使用maven管理jar包。
使用最新的CXF2.7和Spring3的 Java WebService 集成用例 应用Spring Annotation
基于CXF实现JAVA_WebService.doc Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services
基于spring和CXF发布webservice,包含java本地发布和调用及web发布和调用。
spring,cxf,restful发布webservice传递复杂对象,例如List,Map,List<Map>
testWebService 是webservice的服务端 zxntest 是webservice 的客户端(很大一部分jar包无效) http://localhost:8080/testWebService/webservice/webservice?wsdl 测试路径
采用spring4.1+mybatis+cxf搭建的webservice demo
使用cxf和spring开发基于https的webservice服务端以及客户端样例
Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services 各种类型的Annotation。@WebService和@WebMethod是WSDL映射Annatotion。这些Annotation将描述Web ...
将下载的demo(包括serviceserverdemo及serviceclientdemo,bat文件在serviceclientdemo的src下)导入eclipse即可运行使用,编译时可能需要修改jdk版本,如果导入有错,可新建web项目,按所下载demo的结构搭建即可,...
Apache CXF 是一个开放源代码框架...它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。
web项目使用spring和cxf的一个开发实例,有简单的代码样例和jar。是一个完整的项目,最终发布完成时访问 http://ip:port/项目名称/webservices/ 就会发现你发布的webservice服务。
Apache CXF是一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件...CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。
弹簧-CXF-WS 一些使用spring和cxf生成webservice Rest和Soap的例子
java cxf webservice接口解决跨域问题,cors-filter-1.7.jar、java-property-utils-1.9.jar,以及web.xml文件配置
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验
@QueryParam注解是可以抽取并在资源类中使用的一类参数。Query参数是从请求URI的查询参数中抽取的。 @Consumes注解是用来指定资源能够接受的客户发送的MIME媒体类型。 @Produces注解用来指定资源能够生成并发送给...
这个是今天学习的的cxf开发webservice,里面同时含有的dom4j解析xml代码给大家看看~里面jar包都有 直接导入项目就是 cxf我觉得比其他的webservice开发好用多了 不愧称与spring的无缝衔接