1. 程式人生 > >CXF實現WebService對物件和XML檔案的釋出

CXF實現WebService對物件和XML檔案的釋出

搭建工程前文以及介紹

1..........................建立POJO類User.java

package ObjectInterface;

public class User {
	private String username;
	private String phoneNO;
	private int age;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPhoneNO() {
		return phoneNO;
	}
	public void setPhoneNO(String phoneNO) {
		this.phoneNO = phoneNO;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}

2......................建立介面檔案

package ObjectInterface;

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

@WebService
public interface GetUserInfo {
	@WebMethod
	//封裝物件
	public User getUserInfo(@WebParam(name="username")String username);

	//封裝Document資料,返回XML字串
	public String XMLInfo() throws Exception;
}

3.......................建立實現類
package ObjectInterface;


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

import util.OperationXMLByJdom;

public class GetUserInfoImp implements GetUserInfo {

	//封裝物件
	public User getUserInfo(String username) {

		User user = new User();
		user.setUsername(username);
		user.setAge(100);
		user.setPhoneNO("1234567");

		return user;
	}

	//封裝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;

	}


}

(上文用到的OperationXMLByJdom)

3.....................................OperationXMLByJdom

package util;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.StringReader;

import org.jdom.Document;   
import org.jdom.input.SAXBuilder;   
import org.jdom.output.Format;   
import org.jdom.output.XMLOutputter;   
  
 
public class OperationXMLByJdom {   
    /**  
     * DOCUMENT格式化輸出儲存為XML  
     *   
     * @param doc JDOM的Document  
     * @param filePath 輸出檔案路徑  
     * @throws Exception  
     */  
    public static void doc2XML(Document doc, String filePath) throws Exception{   
        Format format = Format.getCompactFormat();    
        format.setEncoding("UTF-8"); //設定XML檔案的字元為UTF-8   
        format.setIndent("     ");//設定縮排    
       
        XMLOutputter outputter = new XMLOutputter(format);//定義輸出 ,在元素後換行,每一層元素縮排四格    
        FileWriter writer = new FileWriter(filePath);//輸出流   
        outputter.output(doc, writer);   
        writer.close();   
    }   
       
    /**  
     * 字串轉換為DOCUMENT  
     *   
     * @param xmlStr 字串  
     * @return doc JDOM的Document  
     * @throws Exception  
     */  
    public static Document string2Doc(String xmlStr) throws Exception {   
        java.io.Reader in = new StringReader(xmlStr);   
        Document doc = (new SAXBuilder()).build(in);          
        return doc;   
    }   
  
    /**  
     * 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();   
    }   
  
    /**  
     * XML轉換為Document  
     *   
     * @param xmlFilePath XML檔案路徑  
     * @return doc Document物件  
     * @throws Exception  
     */  
    public static Document xml2Doc(String xmlFilePath) throws Exception {   
        File file = new File(xmlFilePath);   
        return (new SAXBuilder()).build(file);   
    }   
         
}  


5..............................釋出介面
	<!-- 釋出物件型別介面 -->
	<jaxws:server id="GetUserInfo" serviceClass="ObjectInterface.GetUserInfo"
		address="/GetUserInfo">
		<jaxws:serviceBean>
			<ref bean="userInfo" />
		</jaxws:serviceBean>
	</jaxws:server>

<bean id="userServiceBean" class="com.ComplexUserService" />
	<bean id="userInfo" class="ObjectInterface.GetUserInfoImp"></bean>

6.............................直接啟動伺服器就Ok了  檢視一下WSDL檔案

這裡不再進行客戶端驗證了