json转javabean出错
json转换为javaBean出错
javabean如下
package org.twtpush.dto.BrokerStatus;
import org.twtpush.dto.Broker.Connection;
import java.util.List;
/**
* {
"id": "default",
"state": "STARTING",
"state_since": 1471856089398,
"version": "1.7.1",
"home_location": "/opt/apache-apollo-1.7.1",
"base_location": "/opt/apache-apollo-1.7.1/bin/twt",
"jvm_metrics": {
"heap_memory": {
"used": 75596152,
"alloc": 218103808,
"max": 954728448
},
"non_heap_memory": {
"used": 40754176,
"alloc": 41353216,
"max": 136314880
},
"classes_loaded": 6900,
"classes_unloaded": 0,
"threads_current": 36,
"threads_peak": 41,
"os_arch": "amd64",
"os_name": "Linux 4.4.0-34-generic (Ubuntu 16.04.1 LTS)",
"os_memory_total": 8279302144,
"os_memory_free": 310333440,
"os_swap_total": 0,
"os_swap_free": 15999168512,
"os_fd_open": 147,
"os_fd_max": 100000,
"os_load_average": 0.21,
"os_cpu_time": 11150000000,
"os_processors": 8,
"runtime_name": "11942@nero-Lenovo-Y430P",
"jvm_name": "Java HotSpot(TM) 64-Bit Server VM 1.7.0_80 (Oracle Corporation)",
"uptime": 162504,
"start_time": 1471856086106
},
"current_time": 1471856252952,
"virtual_hosts": [
"twt"
],
"connectors": [
"tcp",
"tls",
"ws",
"wss"
],
"connection_counter": 0,
"connected": 0,
"connections": [],
"messages_sent": 0,
"messages_received": 0,
"read_counter": 0,
"write_counter": 0
}
* Created by nero on 16-8-22.
*/
public class BrokerStatus {
private String id;
private String state;
private int state_since;
private String version;
private String home_location;
private String base_location;
private Jvm_metrics jvm_metrics;
private int current_time;
private List<String> virtual_hosts ;
private List<String> connectors ;
private int connection_counter;
private int connected;
private List<Connection> connections ;
private int messages_sent;
private int messages_received;
private int read_counter;
private int write_counter;
public BrokerStatus(String id, String state, int state_since, String version, String home_location, String base_location, Jvm_metrics jvm_metrics, int current_time, List<String> virtual_hosts, List<String> connectors, int connection_counter, int connected, List<Connection> connections, int messages_sent, int messages_received, int read_counter, int write_counter) {
this.id = id;
this.state = state;
this.state_since = state_since;
this.version = version;
this.home_location = home_location;
this.base_location = base_location;
this.jvm_metrics = jvm_metrics;
this.current_time = current_time;
this.virtual_hosts = virtual_hosts;
this.connectors = connectors;
this.connection_counter = connection_counter;
this.connected = connected;
this.connections = connections;
this.messages_sent = messages_sent;
this.messages_received = messages_received;
this.read_counter = read_counter;
this.write_counter = write_counter;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public void setState(String state){
this.state = state;
}
public String getState(){
return this.state;
}
public void setState_since(int state_since){
this.state_since = state_since;
}
public int getState_since(){
return this.state_since;
}
public void setVersion(String version){
this.version = version;
}
public String getVersion(){
return this.version;
}
public void setHome_location(String home_location){
this.home_location = home_location;
}
public String getHome_location(){
return this.home_location;
}
public void setBase_location(String base_location){
this.base_location = base_location;
}
public String getBase_location(){
return this.base_location;
}
public void setJvm_metrics(Jvm_metrics jvm_metrics){
this.jvm_metrics = jvm_metrics;
}
public Jvm_metrics getJvm_metrics(){
return this.jvm_metrics;
}
public void setCurrent_time(int current_time){
this.current_time = current_time;
}
public int getCurrent_time(){
return this.current_time;
}
public void setVirtualhosts(List<String> virtual_hosts){
this.virtual_hosts = virtual_hosts;
}
public List<String> getVirtual_hosts(){
return this.virtual_hosts;
}
public void setString(List<String> connectors){
this.connectors = connectors;
}
public List<String> getString(){
return this.connectors;
}
public void setConnection_counter(int connection_counter){
this.connection_counter = connection_counter;
}
public int getConnection_counter(){
return this.connection_counter;
}
public void setConnected(int connected){
this.connected = connected;
}
public int getConnected(){
return this.connected;
}
public void setConnections(List<Connection> connections){
this.connections = connections;
}
public List<Connection> getConnections(){
return this.connections;
}
public void setMessages_sent(int messages_sent){
this.messages_sent = messages_sent;
}
public int getMessages_sent(){
return this.messages_sent;
}
public void setMessages_received(int messages_received){
this.messages_received = messages_received;
}
public int getMessages_received(){
return this.messages_received;
}
public void setRead_counter(int read_counter){
this.read_counter = read_counter;
}
public int getRead_counter(){
return this.read_counter;
}
public void setWrite_counter(int write_counter){
this.write_counter = write_counter;
}
public int getWrite_counter(){
return this.write_counter;
}
}
json如下
{"id":"default","state":"STARTING","state_since":1471856089398,"version":"1.7.1","home_location":"/opt/apache-apollo-1.7.1","base_location":"/opt/apache-apollo-1.7.1/bin/twt","jvm_metrics":{"heap_memory":{"used":52671712,"alloc":126877696,"max":954728448},"non_heap_memory":{"used":43066464,"alloc":43581440,"max":136314880},"classes_loaded":6968,"classes_unloaded":0,"threads_current":35,"threads_peak":41,"os_arch":"amd64","os_name":"Linux 4.4.0-34-generic (Ubuntu 16.04.1 LTS)","os_memory_total":8279302144,"os_memory_free":919715840,"os_swap_total":0,"os_swap_free":15999168512,"os_fd_open":140,"os_fd_max":100000,"os_load_average":0.72,"os_cpu_time":238030000000,"os_processors":8,"runtime_name":"11942@nero-Lenovo-Y430P","jvm_name":"Java HotSpot(TM) 64-Bit Server VM 1.7.0_80 (Oracle Corporation)","uptime":64824402,"start_time":1471856086106},"current_time":1471920910619,"virtual_hosts":["twt"],"connectors":["tcp","tls","ws","wss"],"connection_counter":0,"connected":0,"connections":[],"messages_sent":0,"messages_received":0,"read_counter":0,"write_counter":0}
转换带码:
ObjectMapper objectMapper=new ObjectMapper();
BrokerStatus brokerStatus=objectMapper.readValue(EntityUtils.toString(entity), BrokerStatus.class);
log:
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.twtpush.dto.BrokerStatus.BrokerStatus]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: {"id":"default","state":"STARTING","state_since":1471856089398,"version":"1.7.1","home_location":"/opt/apache-apollo-1.7.1","base_location":"/opt/apache-apollo-1.7.1/bin/twt","jvm_metrics":{"heap_memory":{"used":57464656,"alloc":123731968,"max":954728448},"non_heap_memory":{"used":43119848,"alloc":43646976,"max":136314880},"classes_loaded":6969,"classes_unloaded":0,"threads_current":35,"threads_peak":41,"os_arch":"amd64","os_name":"Linux 4.4.0-34-generic (Ubuntu 16.04.1 LTS)","os_memory_total":8279302144,"os_memory_free":609484800,"os_swap_total":0,"os_swap_free":15999168512,"os_fd_open":140,"os_fd_max":100000,"os_load_average":0.63,"os_cpu_time":241880000000,"os_processors":8,"runtime_name":"11942@nero-Lenovo-Y430P","jvm_name":"Java HotSpot(TM) 64-Bit Server VM 1.7.0_80 (Oracle Corporation)","uptime":65720718,"start_time":1471856086106},"current_time":1471921806830,"virtual_hosts":["twt"],"connectors":["tcp","tls","ws","wss"],"connection_counter":0,"connected":0,"connections":[],"messages_sent":0,"messages_received":0,"read_counter":0,"write_counter":0}; line: 1, column: 2]
at
com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1080)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:142)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3564)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2580)
at org.twtpush.service.impl.BrokerServiceImpl.getBrokerStatus(BrokerServiceImpl.java:76)
at org.twtpush.service.IBrokerServiceTest.getBrokerStatus(IBrokerServiceTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的BrokenStatus类没有默认的构造函数,需要加一个
另外你好几个字段的整型超范围了,得改成Long
你需要的是一个无參的构造器,例如:
要么你就配合@JsonCreator,修改你那个已经写好的有參构造器