Search code examples
javajacksondto

Should a dto implement an associated interface?


Should a bean dto always have an associated interface ?

Below dto is used by jackson to send json over the wire :

public class Bean {

    private String date;

    public Bean(String date)
    {
        this.link = date;
    }

    public String getDate() {
        return date;
    }

}

Should this class always implement an interface to match its structure ?


Solution

  • I wouldn't unless you're using an API or Framework that requires an interface or are writing an API yourself.

    Older versions of J2EE (before it became Java EE) required interfaces for enterprise beans, and some other frameworks use an interface to generate a proxy; however that has mostly been replaced by the runtime generation of synthetic proxies. If you start with a well defined class, you can later add an interface you discover a need for one.

    Currently one of my tasks is maintaining an existing web application. Essentially everything in it has the Interface + Class pattern, but for no real reason as its all self contained. The extra files for the interfaces just clutter up the workspace and make tracking down the source of the actual code take a couple seconds longer in each case (can't just highlight and press F3 in Eclipse).