上图就是我用restful接口收到的 对象。转为list报错。
那么我是怎么解决的呢。
步骤3步:
1.我先用ResponseEntity<String> ec = restTemplate.getForEntity("http://hello/returnlog", String.class); 得到json
2.我用jackson mapper, 把json转为对象数组。
com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();
MetricLog[] mlog = mapper.readValue(ec.getBody(), MetricLog[].class);3. 对象数组转list集合
java.util.List<MetricLog> mlogList = java.util.Arrays.asList(mlog);
代码解释如下:
下面是javabean
public class MetricLog implements Serializable {
public String MW_Name;
public String MW_Host;
public Long LId;
public java.util.Date Log_Time;
public java.lang.String Req_QueryId;
public java.lang.String UserId;
public java.lang.String Web_ModuleId;
public java.lang.String Web_Module_Name;
public java.lang.String Log_Level;
public java.lang.String Log_Line;
public java.lang.String Log_Text;
public java.lang.String Log_Error;
public java.lang.String Log_Stack;
public MetricLog() {
this.MW_Host = MetricConstants.MW_Host;
this.MW_Name = MetricConstants.MW_Name;
}
}
这是服务提供方(/returnlog)
package com.broadtech.clientmain.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.broadtech.middleware.metric.MetricLog;
@RestController
public class StartApp {
@Autowired
private RestTemplate template;
@RequestMapping("/hello")
public String hello() {
return "eureka-client 服务提供方";
}
@RequestMapping("/eatko")
public String index() {
System.out.println("调用消费方的提供的 吃饭");
return template.getForEntity("http://eureka-consumer/eat", String.class).getBody();
}
@RequestMapping("/returnlog")
public List<MetricLog> indesx() {
List<MetricLog> li= new ArrayList<MetricLog>();
System.out.println("构造meticlog对象");
for(int i=0;i<10; i++) {
MetricLog ml=new MetricLog();
ml.LId=1234343434l;
ml.Log_Error="4";
ml.Log_Level="3";
ml.Web_Module_Name="质量业务监控模块";
li.add(ml);
}
System.out.println("li.size()= "+li.size());
return li;
}
}
服务消费方:
package com.broadtech.consumer.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.broadtech.middleware.metric.MetricLog;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONObject;
@Controller
@RestController
public class DealMonitorLog {
private static final int List = 0;
@Autowired
RestTemplate restTemplate;
static int a = 0;
@RequestMapping("/ssg")
public List<MetricLog> tes() {
while (true) {
try {
a++;
Thread.sleep(5000);
System.out.println("this is 第 " + a + " 次获取数据!");
ResponseEntity<String> ec = restTemplate.getForEntity("http://hello/returnlog", String.class);
ObjectMapper mapper = new ObjectMapper();
MetricLog[] mlog = mapper.readValue(ec.getBody(), MetricLog[].class);
java.util.List<MetricLog> mlogList = Arrays.asList(mlog);
System.out.println("mlogList=" + mlogList.size());
for (MetricLog a : mlogList) {
System.out.println("循环打印list MW_Name=" + a.MW_Name);
}
return mlogList;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是自己的eureka 注册中心