2014-12-03 69 views
0

我需要建立一個從數據庫返回多個行的Web服務分隔和各行應containt幾個字段以標籤爲界。我使用Netbeans創建了這個ws,它運行在glassfish服務器上。我只設法返回包含由連接值(字段)組成的字符串的標記行。你能給我一些關於如何修改我的回報的建議嗎?Web服務在一行中返回多個值由標籤

現在我的web服務回報如下:

 <?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:extractCSResponse xmlns:ns2="http://ws/"> 
      <ROW xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"><Firstname>Mark</Firstname><Lastname>Thomas</Lastname><ID>1112546</ID></ROW> 
      <ROW xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"><Firstname>Mike</Firstname><Lastname>Jackson</Lastname><ID>1112547</ID></ROW> 
     </ns2:extractCSResponse> 
    </S:Body> 
</S:Envelope> 

我的回報需要是這樣的:

<S:Body> 
     <ns2:extractCSResponse xmlns:ns2="http://ws/"> 
      <ROWS> 
       <ROW> 
        <Firstname>Mark</Firstname> 
        <Lastname>Thomas</Lastname> 
        <IDname>1112546</IDname> 
       </ROW> 
       <ROW> 
        <Firstname>Mike</Firstname> 
        <Lastname>Jackson</Lastname> 
        <IDname>1112547</IDname> 
       </ROW> 
      </ROWS> 
     </ns2:extractCSResponse> 
    </S:Body> 
下面

Java代碼:

@WebService(serviceName = "GetCS2") 
@Stateless() 
public class GetCS2 { 

    /** 
    * This is a sample web service operation 
    */ 
    @WebMethod(operationName = "extractCS") 
    @WebResult(name = "ROW") 
    public List extractCS() 
    { 
     List l = new ArrayList(); 

     //username and password for database connection 
     String userName = "username"; 
     String password = "password"; 

       //db connection 
      try 
      { 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

       String url = "jdbc:sqlserver://172.19.125.222:1433"+";databaseName=Test"; 
       Connection con = DriverManager.getConnection(url, userName, password); 

       Statement stmt = con.createStatement(); 
       ResultSet rs = stmt.executeQuery("select * from list"); 

       //loop through selection 
       while(rs.next()) 
       {    
        l.add("<Firstname>" + rs.getString("fn") + "</Firstname>" + "<Lastname>" + rs.getString("ln") + "</Lastname>" + "<IDname>" + rs.getDate("ID") + "</ID>"); 
       } 

       //close connections 
       stmt.close(); 
       rs.close(); 
       con.close(); 

      } catch(SQLException e) 
      { 
       System.out.println("SQL Exception: " + e.toString()); 
      } 
      catch(ClassNotFoundException cE) 
      { 
       System.out.println("Class Not Found Exception: " + cE.toString()); 
      } 

      return l; 

    } 
} 
+0

我編輯我的回答一個簡單的例子。希望它有幫助 – MihaiC 2014-12-03 12:53:31

回答

2

編輯展示以便鏈接有一天不再有這個信息時的基本示例mation。

我一般都儘量避免通過連接字符串構建xml。這是很容易出錯,可能包含無效xml數據(如果有什麼名稱包含&<或任何其不能出現在xml?其他特殊字符),並難以改變,一旦有新的數據或數據庫結構有新的變化。 看看JAXB映射對象xml文件。

基本上你需要創建一個表示數據庫object和多個的包裝的自定義類。從您的數據庫填充的對象,將它們添加到包裝,然後用JAXB寫信給xml下面是一個例子JAXB Example

@XmlRootElement(name = "Person") 
public class Person{ 
    private String firstName; 
    private String lastName; 
    private String idName; 

    .... 
} 

@XmlRootElement(name = "PersonWrapper") 
public class PersonWrapper{ 
    private List<Person> persons; 

    @XmlElementWrapper(name = "PersonList") 
    @XmlElement(name = "Person") 
    public List getPersons(){ 
     return persons; 
    } 
    .... 
} 

然後在你的代碼,從數據庫中添加resultset到一個列表。然後將該列表添加到包裝。最後從包裝器創建xml文件。

List<Person> personList=new ArrayList<Person>(); 
while(rs.next()) 
{    
    personList.add(new Person(rs.getString("fn"),rs.getString("lastName"),...)); 
} 

PersonWrapper pw=new PersonWrapper(); 
pw.setPersons(personList); 

JAXBContext context = JAXBContext.newInstance(PersonWrapper.class); 
Marshaller m = context.createMarshaller(); 
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

//write to system.out or any other OutputStream (ByteArrayOutputStream) 
m.marshal(pw, System.out); 

//OR write to xml file 
m.marshal(pw, new File("person.xml")); 

如果你想返回String,然後簡單地轉換OutputStreamString並顯示結果。您可以使用ByteArrayOutputStreambaos.toString(desiredEncoding);

+0

謝謝!我成功地以我想要的方式返回了XML。基本上我不得不創建一個新的Person類,我不得不使用諸如:XmlElement,XmlAttribute和XmlSeeAlso(import javax.xml.bind.annotation。*)。 – mamba 2014-12-05 12:36:21

+0

@mamba很高興你能解決它! – MihaiC 2014-12-05 12:38:23

+0

我忘了提及,我還返回了列表,就像你說的。謝謝! – mamba 2014-12-05 12:43:20