您的位置:首页 > 编程语言 > Java开发

CXF创建webservice服务端、客户端,与spring集成

2014-11-06 17:42 531 查看
服务端

1.必须jar包:cxf-2.6.1.jar

2.spring-cxf.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.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" />

<bean id="checksystemserviceimp" class="a.rst.checksystem.server.CheckSystemServiceImp">
</bean>

<jaxws:endpoint id="webservice" implementor="#checksystemserviceimp"
address="/webservice">
</jaxws:endpoint>
</beans>


3. web.xml配置spring-cxf.xml

<listener>
<listener-class>com.chinavvv.util.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-cxf.xml
</param-value>

</context-param>
<!--还需要配置以下servlet-->
<servlet>   
  <servlet-name>CXFService</servlet-name>   
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup> 
 </servlet>
 <servlet-mapping>   
  <servlet-name>CXFService</servlet-name>   
  <url-pattern>/service/*</url-pattern>
 </servlet-mapping>


4. java类、接口如下,包含接口CheckSystemService、接口实现类CheckSystemServiceImp

package a.rst.checksystem.server;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface CheckSystemService {
public void test(@WebParam(name="param") String param);
public String checkWebSer(String aac002,String aac003,String aab301) throws Exception;
}


@WebService(endpointInterface = "a.rst.checksystem.server.CheckSystemService")
public class CheckSystemServiceImp implements CheckSystemService {

public void test(String param) {
System.out.println("001webservice is success ");
System.out.println("002dao is annotationed? "+relectdao);
System.out.println("003url param:"+param);

}

public String checkWebSer(String aac002,String aac003,String aab301)
throws Exception {

Ceshibean bean=new CeshiBean();//这里是自定义的Javabean

return jsontToString(PersonalInfo);// 返回类型设置为json格式的字符串,没有使用Bean

}

private String jsontToString(Object o){
JsonConfig jsonConfig = new JsonConfig();  //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false);  //设置默认忽略
jsonConfig.setExcludes(new String[]{"child","multipartRequestHandler","resultValueMap","servletWrapper","validatorResults"});
//net.sf.json.JSONObject jsonbean = net.sf.json.JSONObject.fromObject(bean,jsonConfig);
JSONObject json = JSONObject.fromObject(o,jsonConfig);
return json.toString();
}

}


测试是否发布成功地址:http://127.0.0.1:8080/service/webservice?wsdl

客户端:

cd d:/java/bin

wsimport - keep -p com.webservice.client http://127.0.0.1:8080/service/webservice?wsdl
用Java api生成客户端包,以上命令将在d:/java/bin目录下生成com.webservice.client包。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: