cxf发布webServices、xml格式

cxf发布webServices

 

1、spring配置文件

<?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-3.0.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="GreetingService" implementor="com.zcz.cxf.service.impl.GreetingServiceImpl" address="/GreetingService"/>
</beans>

 

2、服务器端

    1、接口

package com.zcz.cxf.service;  
  
import javax.jws.WebService;  
  
@WebService   
public interface GreetingService {   
   public String greeting(String userName);  
   public String say(String eat);
   //封装Document数据,返回XML字符串  
   public String XMLInfo() throws Exception;
}  

   2、实现类

package com.zcz.cxf.service.impl;

import javax.jws.WebService;
import com.zcz.cxf.service.GreetingService;
import com.zcz.cxf.util.OperationXMLByJdom;

import org.jdom.Document;  
import org.jdom.Element;

@WebService
public class GreetingServiceImpl implements GreetingService {
	public String greeting(String userName) {
		return "你好! " + userName;
	}

	public String say(String eat) {
		return "该吃饭了" + eat;
	}
	
	//封装Document数据,返回XML字符串  
        public String XMLInfo() throws Exception {  
            // 创建根节点 list;  
            Element root = new Element("list");  
            // 根节点添加到文档中;  
            Document Doc = new Document(root);  
            // 此处 for 循环可替换成 遍历 数据库表的结果集操作;  
            for (int i = 0; i < 5; i++) {  
                // 创建节点 user;  
                Element elements = new Element("user");  
                // 给 user 节点添加属性 id;  
                elements.setAttribute("id", "" + i);  
                // 给 user 节点添加子节点并赋值;  
                // new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui  
                // 替换成表中记录值;  
                elements.addContent(new Element("name").setText("xuehui"));  
                elements.addContent(new Element("age").setText("28"));  
                elements.addContent(new Element("sex").setText("Male"));  
                // 给父节点list添加user子节点;  
                root.addContent(elements);  
            }  
            OperationXMLByJdom xml2String = new OperationXMLByJdom();  
            String xmlDoc = xml2String.doc2String(Doc);  
            return xmlDoc;
        }  
}

 

3、将document转换为string

package com.zcz.cxf.util;

import org.jdom.Document;
import java.io.ByteArrayOutputStream;    
import org.jdom.output.Format;     
import org.jdom.output.XMLOutputter;  

/**
 * 将document转换为string
 * @author Administrator
 *
 */
public class OperationXMLByJdom {
   /**   
     * Document转换为字符串   
     * @param xmlFilePath XML文件路径   
     * @return xmlStr 字符串   
     * @throws Exception   
     */    
    public static String doc2String(Document doc) throws Exception {     
        Format format = Format.getPrettyFormat();     
        format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8,解决中文问题     
        XMLOutputter xmlout = new XMLOutputter(format);     
        ByteArrayOutputStream bo = new ByteArrayOutputStream();     
        xmlout.output(doc, bo);     
        return bo.toString();     
    }     
}

 

3、客服端

package com.zcz.cxf.client;  
  
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
import com.zcz.cxf.service.GreetingService;
  
public class ClientTest{  
    public static void main(String[] args) {  
    	//创建WebService客户端代理工厂    
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    
        //注册WebService接口    
        factory.setServiceClass(GreetingService.class);    
        //设置WebService地址    
        factory.setAddress("http://localhost:8080/testWebService/cxf/GreetingService");    
        GreetingService greetingService = (GreetingService)factory.create();    
        System.out.println("开始调用webservice...");    
        System.out.println("返回的信息是:"+greetingService.say("米饭")); 
        try {
        	System.out.println("返回的信息是:"+greetingService.XMLInfo());  
	    } catch (Exception e) {
			
	    }
    }  
} 

 

猜你喜欢

转载自x125858805.iteye.com/blog/2028159