CXF+spring demo 以及避免Could not send Message 方法

今天没事就写了一个cxf与spring结合的demo
分享一下:

服务端接口:
package org.tomas.jiang.webservices;

import javax.jws.WebService;

@WebService //指明此接口的方式为webservice
public interface IHelloWorld
{
public String sayHello(String text);

}


调用接口:

package org.tomas.jiang.webservices;

import javax.jws.WebService;

@WebService(endpointInterface="org.tomas.jiang.webservices.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {

@Override
public String sayHello(String text) {
// TODO Auto-generated method stub
return "Hello" + text;
}

}


ok,下面开始配置服务端的xml,讲到web.xml,我之前遇到Could not send Message 的问题,因为soap消息发布出去导致的,在web.xml中配置CXF会有一段映射,下面我介绍一下:




CXFService
org.apache.cxf.transport.servlet.CXFServlet
1


CXFService
/services/*



这里需要添加一个前缀/services/当然这个services是我在项目中定义的,大家可以任意定义,为什么要添加呢,我不添加行不行呢,我自己试过,不添加的话,我的tomcat可以正常启动,但是项目无法打开。苦恼,如果有高手希望指点一下。

这里的services是根据客户端的CXF配置来的








刚说道服务端,我们的接口和web.xml都配置完成。开始配置spring





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.xsd

http://cxf.apache.org/jaxws

http://cxf.apache.org/schemas/jaxws.xsd">












这些都是CXF与spring整合必要的东东。

启动项目打开浏览器

http://127.0.0.1:8088/spring3test/services/HelloWorld?wsdl


可以看到,我们已经发布成功了,可以看到wsdl的界面了!

我们把此页面保存为wsdl格式于本地

服务端的事情已经ok咯。


现在我们找到CXF的工具,cmd进入到CXF得bin目录下:


命令 : wsdl2java 这个wsdl文件名


客户端,新建一个项目如wsclient:

此目录下回生成一个包,直接扔到客户端的src目录下


客户端新建一个xml:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
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/schema/jaxws.xsd">

factory-method="create" />








test:


package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.tomas.jiang.webservices.IHelloWorld;

public class Test {


public static void main(String args[])
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
IHelloWorld client = (IHelloWorld) ctx.getBean("client");
String result = client.sayHello("你好!");
System.out.println(result);
}

}


控制台打印:

2012-4-27 10:11:17 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd5b: startup date [Fri Apr 27 10:11:17 CST 2012]; root of context hierarchy
2012-4-27 10:11:17 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-client.xml]
2012-4-27 10:11:18 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@11d0a4f: defining beans [client,clientFactory]; root of factory hierarchy
2012-4-27 10:11:19 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservices.jiang.tomas.org/}IHelloWorldService from class org.tomas.jiang.webservices.IHelloWorld
Hello你好!


已经ok咯

谢谢

你可能感兴趣的:(CXF+spring demo 以及避免Could not send Message 方法)