Advance JSON
Sometimes you will not be able to achieve mapping of JSON to objects that easily in your application. It is recommended to use a serializer and deserializer in dropwizard.
Example
@JsonSerialize(using=FunkySerializer.class)
@JsonDeserialize(using=FunkyDeserializer.class)
private class Funky {
// ...
}

You can also try this code with Online Java Compiler
Run Code
Snake_Case
@JsonSnakeCase annotation before a class automatically converts the fields snake_case to camleCase and vice versa.
This is important to understand because, in dropwizard, JSON disagrees with snake_case and camelCase field names. After all, it depends on the user which language they are applying.
Example
@JsonSnakeCase
public class Country {
private final String countryName;
@JsonCreator
public Person(@JsonProperty String countryName) {
this.countryName =countryName;
}
@JsonProperty
public String getcountryName() {
return countryName;
}
}

You can also try this code with Online Java Compiler
Run Code
JSON format:
{
“country_name”: “India”
}
Country name in camelCase object will be converted into snake_case and vice-versa.
Unknown Properties
It will be ignored if a JSON property cannot be mapped into a Java property in dropwizard.
Streaming Output
Suppose you want to build an application that returns a lot of information and enhances its performance and efficiency to provide a better user experience. To achieve this, we can use the StreamingOutput interface. In StreamingOutput, the response entity can be streamed using your method in a chunk-encoded output stream. In any other case, you'll need to fully create your return value before passing it to the client.
HTML Representation
If you want to display HTML pages, we can use views in dropwizard. You can get straightforward, quick HTML views using either dropwizard-views-mustache & dropwizard-views-freemarker modules.
Add the ViewBundle to your Application class's initialize method to allow views for your application:
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
bootstrap.addBundle(new ViewBundle<MyConfiguration>());
}

You can also try this code with Online Java Compiler
Run Code
Custom Representation
There will be scenarios when you have an unstructured output format that you need to implement, which cannot be acceptable by JSON. So, in this case, you can apply custom representation. We can achieve this by developing classes compatible with MessageBodyReaderT> and MessageBodyWriterT> interfaces; you can add support for arbitrary input and output formats. As long as they are annotated with @Provider and @Produces("text/gibberish") or @Consumes("text/gibberish"), they are acceptable.
Frequently Asked Questions
What does POJO stand for?
POJO stands for plain old java object.
Why do we recommend JSON format in dropwizard?
The reason to use JSON format in dropwizard is that it is heavily favorable by dropwizard.
Can we map POJO to any other format rather than JSON?
Yes, it is possible in dropwizard to map custom format to POJO and vice-versa.
Can we configure unknown property behavior?
Yes, it can be configured with the help of Dropwizard's object mapper.
Can the snake_case object also be converted to camelCase?
Yes, it is possible with @JsonSnakeCase annotation.
Conclusion
In this blog, we learned about dropwizard Representation classes and many other annotations we can use to manipulate the format according to our necessity. We have learned about POJO to JSON conversion and Advance JSON properties. We have also discussed HTML and Custom Representation in dropwizard.
To learn more about dropwizard, you can check out the following articles:
To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.
Happy Learning!