我是 Spring 新手。编写了非常简单的代码,以便从http://api.engin.umich.edu/hostinfo/...PONT&room=B505的API调用中获取JSON对象的数组
仅获得“名称:NULL”
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Computer[] computer = restTemplate.getForObject("http://api.engin.umich.edu/hostinfo/v1/computers.json?building=PIERPONT&room=B505", Computer[].class);
System.out.println("Name: " + computer[0].getName());
}
}
简单的计算机类(class)就在这里。
package hello;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Computer {
private String hostname;
public String getName() {
return hostname;
}
public String toString() {
return "Computer [hostname=" + hostname + "]";
}
}
最佳答案
经过测试,可以正常工作!
一些观察:
Spring
不太了解framework
,那就没关系。可以通过多种方式完成serialize/deserialize
。 JSON
包含一些JSON
单词,例如reserved
或class
。为了处理此问题,您将需要使用string
批注(稍后您将在@JsonProperty
和其他站点上看到它)。我还使用了此批注,而不是声明Computer.java
属性中包含下划线的variables
(请记住,您的JSON
DTO's
必须具有相同的[beans]
,structure
,并且还必须使用相同的datatypes
属性名称)。 JSON
的问题RestTemplate
(我不确定使用object
时是否使用正确的方法)。作为建议,请考虑以下/检查此Computer[].class
代码段:https://stackoverflow.com/a/6349488/1178686 代码:
code
DTO's
:[beans]
,Computer.java
,Load.java
和一个测试Location.java
:class
。请检查代码! 计算机.java
package com.example.dto;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
public class Computer {
private String hostname;
private String vendor;
private String model;
private int swap;
private int memory;
@JsonProperty("sess_count")
private int sessCount;
private List<Load> load;
@JsonProperty("in_use")
private boolean inUse;
@JsonProperty("free_in_tmp")
private int freeInTmp;
@JsonProperty("class")
private String clazz;
private List<Location> location;
@JsonProperty("last_access")
private String lastAccess;
@JsonProperty("last_report")
private int lastReport;
private String ip;
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getSwap() {
return swap;
}
public void setSwap(int swap) {
this.swap = swap;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
public int getSessCount() {
return sessCount;
}
public void setSessCount(int sessCount) {
this.sessCount = sessCount;
}
public List<Load> getLoad() {
return load;
}
public void setLoad(List<Load> load) {
this.load = load;
}
public boolean isInUse() {
return inUse;
}
public void setInUse(boolean inUse) {
this.inUse = inUse;
}
public int getFreeInTmp() {
return freeInTmp;
}
public void setFreeInTmp(int freeInTmp) {
this.freeInTmp = freeInTmp;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public List<Location> getLocation() {
return location;
}
public void setLocation(List<Location> location) {
this.location = location;
}
public String getLastAccess() {
return lastAccess;
}
public void setLastAccess(String lastAccess) {
this.lastAccess = lastAccess;
}
public int getLastReport() {
return lastReport;
}
public void setLastReport(int lastReport) {
this.lastReport = lastReport;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
@Override
public String toString() {
String str = "=================================\r\n";
str += "Hostname: " + hostname + "\r\n" +
"Vendor: " + vendor + "\r\n" +
"Model: " + model + "\r\n" +
"Swap: " + swap + "\r\n" +
"Memory: " + memory + "\r\n" +
"Sess_Count: " + sessCount + "\r\n" +
"Load: " + "\r\n";
for(Load ld : load) {
str += "\t" + "One: " + ld.getOne() + "\r\n";
str += "\t" + "Five: " + ld.getFive() + "\r\n";
str += "\t" + "Fifteen: " + ld.getFifteen() + "\r\n";
}
str += "In_Use: " + inUse + "\r\n" +
"Free_In_Tmp: " + freeInTmp + "\r\n" +
"Class: " + clazz + "\r\n" +
"Location: " + "\r\n";
for(Location lc : location) {
str += "\t" + "Building: " + lc.getBuilding() + "\r\n";
str += "\t" + "Computers: " + lc.getComputers() + "\r\n";
str += "\t" + "Room: " + lc.getRoom() + "\r\n";
str += "\t" + "String: " + lc.getStr() + "\r\n";
}
str += "Ip: " + ip + "\r\n";
str += "Last_Access: " + lastAccess + "\r\n";
str += "Last_Report: " + lastReport + "\r\n";
return str;
}
}
加载.java
package com.example.dto;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Load {
private int one;
private int five;
private int fifteen;
public int getOne() {
return one;
}
public void setOne(int one) {
this.one = one;
}
public int getFive() {
return five;
}
public void setFive(int five) {
this.five = five;
}
public int getFifteen() {
return fifteen;
}
public void setFifteen(int fifteen) {
this.fifteen = fifteen;
}
}
Location.java
package com.example.dto;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonProperty;
@XmlRootElement
public class Location {
private String room;
private String building;
private String computers;
@JsonProperty("string")
private String str;
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getBuilding() {
return building;
}
public void setBuilding(String building) {
this.building = building;
}
public String getComputers() {
return computers;
}
public void setComputers(String computers) {
this.computers = computers;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
JSONParse.java:可运行的
JSONParse.java
,仅用于测试目的。package com.example.main;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.example.dto.Computer;
public class JSONParse {
public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException {
/* JSON provider */
URL url = new URL("http://api.engin.umich.edu/hostinfo/v1/computers.json?building=PIERPONT&room=B505");
ObjectMapper mapper = new ObjectMapper();
/*
* This allows the ObjectMapper to accept single values for a collection.
* For example: "location" property in the returned JSON is a collection that
* can accept multiple objects but, in deserialization process, this property just
* have one object and causes an Exception.
*/
mapper.configure(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
/*
* If some JSON property is not present, avoid exceptions setting
* FAIL_ON_UNKNOWN_PROPERTIES to false
*/
mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
/* Get all computers */
List<Computer> computers = mapper.readValue(url, new TypeReference<List<Computer>>(){});
/* Print each computer (previously overriding "toString()" method) */
for(Computer computer : computers) {
System.out.println(computer.toString());
}
}
}
上面的
class
的输出:享受!