我需要建立一個從數據庫返回多個行的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;
}
}
我編輯我的回答一個簡單的例子。希望它有幫助 – MihaiC 2014-12-03 12:53:31