Table of contents
1.
Introduction
2.
Representation Classes
2.1.
Example
2.2.
JSON Format
3.
Advance JSON
3.1.
Example
3.2.
Snake_Case
3.2.1.
Example
3.3.
Unknown Properties
4.
Streaming Output
5.
HTML Representation
6.
Custom Representation
7.
Frequently Asked Questions
7.1.
What does POJO stand for?
7.2.
Why do we recommend JSON format in dropwizard?
7.3.
Can we map POJO to any other format rather than JSON?
7.4.
Can we configure unknown property behavior?
7.5.
Can the snake_case object also be converted to camelCase?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Dropwizard-Representation

Author dhananjay
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog's content will help you understand the Representation classes in dropwizard and the various features and modules available in Representation classes. Before we jump into logging, let's have a brief about dropwizard.

Dropwizard is a java based web framework that is used to create high-performance RESTFUL web services,s and is open source.

Dropwizard-Representation

Representation Classes

When handled by different Jersey MessageBodyReader and MessageBodyWriter providers, representation classes are those classes turning into the entities in your application's API. Dropwizard is compatible with JSON format, but we can do the mapping to POJO with any suitable custom format and vice-versa.

Example

Converting POJO to JSON with @JsonProperty.

public class Greeting {

    @JsonProperty
    private String greeting;

    public Greeting() {
    }

    public Greeting(String greeting) {
        this.greeting = greeting;
    }

    public String getGreeting() {
        return greeting;
    }
}
You can also try this code with Online Java Compiler
Run Code

JSON Format

{
	“greeting”: “hello, welcome to coding ninjas.”
}

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!

Live masterclass