3

I am working on Springboot MySQL example(Similar example). In one of the methods, I want to log JSON data but I am getting,

com.example.employee.model.Employee@1595ddd2

@RequestMapping(value="/employees12/{id}", method=RequestMethod.GET)
public Employee getPerson(@PathVariable Long id){
    Employee employee = employeeRepository.findOne(id);
    //String str=employee.toString();
    //System.out.println("string is " + str);
    System.out.println(employee); //print json in logs console
    return employee;
}

The return employees; is giving JSON data. I have tried toString(), even that doesnt work. Any help is appreciated.

John Seen
  • 701
  • 4
  • 15
  • 31
  • 2
    @Reimeus: This is not a correct link to duplicate. I think this question should not be closed at all. The OP is definitely aware of how `System.out::println` and `Object::toString` works. He needs to log the outgoing JSON response. Please consider reopening the question. – Nikolas Charalambidis Aug 24 '18 at 20:32

3 Answers3

6

You can use writerWithDefaultPrettyPrinter from ObjectMapper. This enables pretty print.

private ObjectMapper mapper = new ObjectMapper();

@RequestMapping(value="/employees12/{id}", method=RequestMethod.GET)
public Employee getPerson(@PathVariable Long id){
    Employee employee = employeeRepository.findOne(id);
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee));
    return employee;
}

If you want the output just in compact mode, use writeValueAsString

System.out.println(mapper.writeValueAsString(employee));
1218985
  • 7,531
  • 2
  • 25
  • 31
  • Thanks for the solution, it is working as expected. I am a non-Java guy and was searching for a solution for the last couple of hours. I did not know about this ObjectMapper PrettyPrinter concept. – John Seen Aug 24 '18 at 20:56
1

In the getPerson() method, use objectMapper.writeValueAsString() to get the JSON of the employee object:

@RequestMapping(value="/employees12/{id}", method=RequestMethod.GET)
public Employee getPerson(@PathVariable Long id){
    Employee employee = employeeRepository.findOne(id);
    ObjectMapper objectMapper = new ObjectMapper();
    System.out.println(objectMapper.writeValueAsString(employee));
    return employee;
}

Adding a toString() in Employee class, with the ObjectMapper from Jackson to serialize the Employee instance. The advantage of overriding the toString() method in the Employee class is you can just do System.out.println(employee); anywhere to get the JSON representation of the Employee object.

 public String toString(){
     String serialized ="";
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            serialized = objectMapper.writeValueAsString(this);
        }catch(JsonProcessingException jpe){
            jpe.printStackTrace();
        }
    return serialized;
 }
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • 1
    It returns Object parsed to string with `{`, `/` `}` chars e.g,: `"{\"street\":\"scEEqbJsKXIhmdXzzjeXxvLlWSMCdz\",\"buildingNumber\":\"cDlmnRjhBJDyZcDgnb\",\"city\":\"f"` – BartusZak Mar 21 '22 at 21:31
-1

You can use google gson library:

    @Override
public String toString() {
    return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}
user666
  • 1,750
  • 3
  • 18
  • 34