gpt4 book ai didi

java - 将对象转换为字符串时出错

转载 作者:行者123 更新时间:2023-11-30 03:03:35 24 4
gpt4 key购买 nike

在我的 Controller 类中,我使用 ObjectMapper 读取 JSON 数据,然后将这些值存储在 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():

public String toString() {
return "message: " + message +
",\n data: " + data +
",\n datetime: " + datetime;
}

关于java - 将对象转换为字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35359568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com