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.

1
  • 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. Commented Aug 24, 2018 at 20:32

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

It returns Object parsed to string with {, / } chars e.g,: "{\"street\":\"scEEqbJsKXIhmdXzzjeXxvLlWSMCdz\",\"buildingNumber\":\"cDlmnRjhBJDyZcDgnb\",\"city\":\"f"
-1

You can use google gson library:

    @Override
public String toString() {
    return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}

1 Comment

or reflection using apache lang

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.