在我的控制器類m使用ObjectMapper讀取JSON數據,以及dat m將這些值存儲在MyPojo類中之後,現在我想要打印POJO對象中存在的內容。如果我在做system.out.println(pojo);
它顯示一些十六進制值。那麼,我應該怎麼做才能獲得內容。將對象轉換爲字符串時出錯
這是我的模型類:
package com.ge.health.model;
import java.util.Date;
public class JSONmodel {
private String message;
private String data;
private String datetime;
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
這是我的主類Controller類
package com.ge.health.poc.controller;
import java.io.IOException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ge.health.model.JSONmodel;
@RestController
public class JSONSendController {
@RequestMapping(value="/messagejson",method= RequestMethod.POST)
@ResponseBody
public void helloService(@RequestBody(required = false) String input) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JSONValidationClass validate=new JSONValidationClass();
System.out.println("Service Says 123 - " + input);
boolean retValue = validate.isValidJSON(input);
System.out.println("this is the value:"+retValue);
JSONmodel pojodata = mapper.readValue(input, JSONmodel.class);
System.out.println(pojodata);
System.out.println(pojodata.toString());
}
}
好了,你還沒有覆蓋'的toString()'的'JSONModel'類。你期望字符串表示是什麼,以及你期望這樣做的代碼是什麼? –
可能重複[如何打印我的Java對象而不會得到「SomeType @ 2f92e0f4」?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-越來越 - sometype2f92e0f4) – Tom