Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Model Objects from External API

I'm new in Play 2 Framework v. 2.1.1 with Java and I'm looking for the best way to do the following without duplicating code.

To simplify, I have a Play 2 backoffice that uses an external API. I don't manage this API, but I call REST Services to perform operations over the api.

This API's objects are exactly the same as Play 2 Model Objects. But I don't want to duplicate the api objects to add Play validations and other annotations.

Is there any way to add this type of behavior using configuration files? I'm thinking about something like Hibernate hbm's for example.

For example:

Object in the unmanaged api: (I omit getters and setters for simplicity)

public class Entity{
    public String field1;
    public String field2;
}

Object that I want to avoid: (I omit getters and setters for simplicity)

public class Entity1{

    @Required
    @NonEmpty
    @MinLength(3)
    public String field1;

    @Required
    @NonEmpty
    public String field2;
}

Config example: (I need something like this)

<class name="Entity1">
    <property name="field1" >
        <required/>
        <nonEmpty/>
        <minLength value="3"/>
    </property>
    <property name="field2" >
        <required/>
        <nonEmpty/>
    </property>
</class>

Using annotations seems better than using xmls or any other configuration file, so I don't necessarily want to use configuration files, I'm open to any suggestions to solve this problem.

Thanks

like image 305
Diego D Avatar asked May 27 '13 20:05

Diego D


1 Answers

I can't see how duplicating the API model in a non typesafe descriptor like XML is better than using a typesafe language. Moreover, I would not want to couple my model and application to a model from the API under my control.

I think it is far better to duplicate the model in Java/Scala and use a simple bean copier like dozer to move between the two.

like image 76
dres Avatar answered Nov 07 '22 09:11

dres