1

In my controller class m reading JSON data using ObjectMapper and after dat m storing these values in MyPojo class and now i want to print the content which is there in the POJO object. If I am doing system.out.println(pojo); it is showing some hexadecimal value. So what should I do to get thee content.

This is my model class:

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;
    }
}

This is my main class Controller class

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());
    }
}
kryger
  • 12,906
  • 8
  • 44
  • 65
Priyanka Taneja
  • 157
  • 2
  • 3
  • 13
  • 2
    Well, you haven't overridden `toString()` in the `JSONModel` class. What did you expect the string representation to be, and which code did you expect to do that? – Jon Skeet Feb 12 '16 at 10:00
  • 1
    Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Tom Feb 12 '16 at 10:31

3 Answers3

2

You need to override toString() with something that tells you what you want to know:

public String toString() {
        return "message: " + message +
                ",\n data: " + data + 
                ",\n datetime: " + datetime;
}
Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
0

Invoking System.out.println on an instance of your object will print its String representation as implemented by toString.

If you don't override Object#toString, you'll be printing your object in the [ClassName@hashCode] notation, just as implemented in Object.

In this case, your issue is slightly confusing because you are de-serializing an instance of your pojo from JSON, but what you are actually printing is that instance, so as long as toString is not overridden, you will get "some hexadecimal value" as you mention.

Mena
  • 47,782
  • 11
  • 87
  • 106
0

You have to override(generate) the toString method in JSONmodel class.

Like:

@Override
public String toString() {
    return "JSONmodel [message=" + message + ", data=" + data
            + ", datetime=" + datetime + "]";
} 
Tom
  • 16,842
  • 17
  • 45
  • 54
Pallavi
  • 652
  • 1
  • 10
  • 26