我有以下服务返回对象的arraylist,我创建了restful客户端来返回这个arraylist但它返回如下代码的字符串,现在我想将它作为对象的arraylist检索。
返回对象的arraylist的Web服务代码
@RequestMapping(value = "/getQuery",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<User> get() throws JSONException {
return userRepository.findAll();
}
jersy client
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/myproject/api/getQuery");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
String output = response.getEntity(String.class);
//how to get arraylist from responce
答案 0 :(得分:1)
您可以像这样使用。 我的restservice正在发送Arraylist列表。我将这种方法用于restclient来从restservice引入arraylist数据
public static void getAllData() {
String uri = "http://localhost:8185/staff";
try {
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
Client client = Client.create(clientConfig);
WebResource resource = client.resource(uri);
ClientResponse response = resource.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
if (response.getStatus() == 200) {
ArrayList<Staff> list = response.getEntity(new GenericType<ArrayList<Staff>>() {});
for (Staff staff : list) System.out.println(staff);
}
} catch (Exception e) { System.out.println("Exception is:" + e.getMessage()); }
}
我的实体是这个。
@XmlRootElement()
public class Staff {
private Long staffid;
private String staffname;
private String stafflastname;
private String gender;
private String email;
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Long getStaffid() {
return staffid;
}
public void setStaffid(Long staffid) {
this.staffid = staffid;
}
public String getStaffname() {
return staffname;
}
public void setStaffname(String staffname) {
this.staffname = staffname;
}
public String getStafflastname() {
return stafflastname;
}
public void setStafflastname(String stafflastname) {
this.stafflastname = stafflastname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Staff [staffid=" + staffid + ", staffname=" + staffname + ", stafflastname=" + stafflastname
+ ", gender=" + gender + ", email=" + email + ", city=" + city + "]";
}
}
答案 1 :(得分:0)
通常,建议从服务器发送JSON或XML响应,并在客户端对其进行操作。
试试这个,
休息服务
public Response saveDataIntoHash() {
List<User> us= userRepository.findAll();
GenericEntity generic = new GenericEntity<List<User>>(us){};
return Response.status(201).entity(generic).build();
}
这是客户端
Client c = Client.create(config);
WebResource resource = c.resource(Main.BASE_URI);
ClientResponse response = resource.path("getQuery")
.accept("application/json").get(ClientResponse.class);
List<User> users
= response.getEntity(new GenericType<List<User>>(){});
StringBuilder builder = new StringBuilder("=== User===\n");
for (User u: users) {
builder.append("Name: ").append(u.getName()).append("\n");
}
builder.append("==================");
System.out.println(builder.toString());