One of the goodies of vertx-jooq is that it allows you to automatically convert POJOs and jOOQ-Records from and into JSON-Objects. Until now however, the key of the generated JSON-field was fixed to the POJO’s member name that represents that column (which stays the default). Starting with version 2.3.5 this behavior can be changed in different ways:
- Subclass the
VertxGenerator
of your choice and overwritegetJsonKeyName
-method. - Subclass
VertxGeneratorStrategy
of your choice and overwritegetJsonKeyName
-method. - Set a different delegate used by the
VertxGeneratorStrategy
(see also #6) and change the way how the POJO’s member names are rendered.
Imagine you want the JSON keys to be exactly the same as their database column counterparts. Here is how you would do it in all three different methods:
Option 1:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassicAsInDBVertxGenerator extends ClassicVertxGenerator { | |
@Override | |
protected String getJsonKeyName(TypedElementDefinition<?> column) { | |
return column.getName(); | |
} | |
} |
Option 2:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassicAsInDBGeneratorStrategy extends ClassicGeneratorStrategy { | |
@Override | |
public String getJsonKeyName(TypedElementDefinition<?> column) { | |
return column.getName(); | |
} | |
} |
Option 3:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClassicAsInDBVertxGenerator extends ClassicGeneratorStrategy{ | |
public ClassicAsInDBVertxGenerator() { | |
super(ClassicVertxGenerator.VERTX_DAO_NAME, new MemberAsInDBGeneratorStrategy()); | |
} | |
static class MemberAsInDBGeneratorStrategy extends DefaultGeneratorStrategy{ | |
@Override | |
public String getJavaMemberName(Definition definition, Mode mode) { | |
if(mode==Mode.POJO){ | |
return definition.getName(); | |
} | |
return super.getJavaMemberName(definition, mode); | |
} | |
} | |
} |
Whatever way you prefer, you also need to alter your code generation configuration and set the correct Generator
/GeneratorStrategy
. Checkout the github page for a how-to or if you’re new to vertx-jooq.
One thought on “vertx-jooq 2.3.5 gives you control over the generated JSON”