服务提供商,2.web.xml 3.pojo类.. 如何为下面的提供者编写客户端...我在数据库中以xml的形式从提供者端获取输出,所以我想在客户端,如何在休息时写客户端?
package sample.first;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("resttest")
public class airtime {
static ResultSet rs = null;
static String msg = "hello";
static Connection dbConnection = null;
Statement st;
@GET
@Path("get_users")
@Produces("application/xml")
// Method for getting iduser
public List<userData> get_users() {
List<userData> retUser = new ArrayList<userData>();
try {
dbConnection = JDBCConnection.getDBConnection();
st = dbConnection.createStatement();
rs = st.executeQuery("SELECT iduser FROM user11");
System.out.println(rs);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
System.out.println("entered while loop");
int numColumns = rsmd.getColumnCount();
userData toReturn = new userData();
for (int i = 1; i < numColumns + 1; i++) {
System.out.println("entered for loop");
System.out.println(numColumns);
String column_name = rsmd.getColumnName(i);
System.out.println("column name: " + column_name);
int id = rs.getInt("iduser");
System.out.println("id=" + id);
if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
System.out.println("Integer working");
toReturn.iduser = rs.getInt(column_name);
}
else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
System.out.println("Varchar working");
toReturn.name = rs.getString(column_name);
}
}
retUser.add(toReturn);
}
dbConnection.close();
return retUser;
} catch (Exception e) {
System.out.println(e);
}
return retUser;
}
@GET
@Path("get_all")
@Produces("application/xml")
// method for getting iduser and name(Multiple columns)
public List<userData> get_All() {
List<userData> retUser = new ArrayList<userData>();
try {
dbConnection = JDBCConnection.getDBConnection();
st = dbConnection.createStatement();
rs = st.executeQuery("SELECT iduser,name FROM user11");
System.out.println(rs);
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
System.out.println("entered while loop");
int numColumns = rsmd.getColumnCount();
userData toReturn = new userData();
for (int i = 1; i < numColumns + 1; i++) {
System.out.println("entered for loop");
System.out.println(numColumns);
String column_name = rsmd.getColumnName(i);
System.out.println("column name: " + column_name);
if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
System.out.println("Integer working");
toReturn.iduser = rs.getInt(column_name);
}
else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
System.out.println("Varchar working");
toReturn.name = rs.getString(column_name);
}
}
retUser.add(toReturn);
}
dbConnection.close();
return retUser;
} catch (Exception e) {
System.out.println(e);
}
return retUser;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Restdyamicnew</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>sample.first;org.codehaus.jackson.jaxrs;javax.xml.parsers</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
package sample.first;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class userData {
public String name;
public Integer iduser;
}
答案 0 :(得分:0)
您可以使用Apache HTTPClient或Jersey等任何REST客户端发出请求。
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet('http://.../resttest/get_users');
HttpResponse response = client.execute(request);
由于您的服务正在返回xml,因此您的响应标头将被正确设置(application / xml),而response-body将包含xml。