Spring中使用CXF java的webservice

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值