With love from POJO/DTO – JAVA

It’s now few months ago I found the POJO/DTO way of serializing and deserializing of JSON and AVRO messages. Now using it everywhere. It’s just as easy as ‘describe’ your json message in a Java Class. Initize it. Fill it with your data and Map it to the Jackson ObjectMapper and ready to go.

Create 3 classes:

class NameDTO{
	String Name;
class EmailDTO{
	String email;
class TopdeskDTO{
	EmailDTO callerLookup;
	String status;
	String briefDescription;
	String request;
	NameDTO callType;
	NameDTO category;
	NameDTO subcategory;

Generate the needed getters and setters, for a new Topdesk ticket payload. Actually generating a json string is now no more than:

  TopdeskDTO dto = new TopdeskDTO();
  dto.setCallerLookup(new EmailDTO("mymail@mydomain.com"));
  dto.setBriefDescription("some brief description");
  dto.setRequest("Information about the issue, can even be something in base html");
  dto.setCallType(new NameDTO("Incident")); //fixed value
  dto.setCategory(new NameDTO("Valid Category from Topdesk"));
  dto.setSubcategory(new NameDTO("Valid Subcategory from Topdesk"));

  ObjectMapper mapper = new ObjectMapper();
  String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dto);

This results in this json string:

  "callerLookup" : {
    "email" : "mymail@mydomain.com"
  "status" : "open",
  "briefDescription" : "some brief description",
  "request" : "Information about the issue, can even be something in base html",
  "callType" : {
    "name" : "Incident"
  "category" : {
    "name" : "Valid Category from Topdesk"
  "subcategory" : {
    "name" : "Valid Subcategory from Topdesk"

Fun thing with such a POJO is, that you can also use it in a reversed way. Map your (matching) json on the POJO and and the data is available in java.

String json = {[...]};

ObjectMapper mapper = new ObjectMapper();
TopdeskDTO dto = mapper.readValue(json, TopdeskDTO.class);

Now the dto contains the data from the json string. Input can be anything. In this case it’s a string, but in most of the solutions, the input is of type inputStream (a file reader).