1

I'm looking for a way to create a webservice with jersey/resteasy without annotations in the POJO/model classes. I generate my model classes with jooq and the model classes are generated each time a change is made in the database.

I found two topics related to my problem: here and here but I would like to know if there is another solution than using another lib/implementation.

any tips? technic? idea?

thank you very much!

EDIT: I add some codes. I have a model class called 'Mushroom' and that consists of 3 fields: mushroom_id, title, description.

I would like to have a JSON representation that looks like:

{
    mushroom: {
        mushroom_id: 12,
        title: "pied de mouton",
        description: "très bon champignon!"
    }
}

for that, with Resteasy, I need a mushroom class with the following annotations:

@XmlRootElement(name = "mushroom")
public class Mushroom {
    private Integer mushroomId;
    private String  title;
    private String  description

    // for each field I need a getter
    // with the XmlElement annotations.
    // for 'description' for instance:
    @XmlElement
public String getDescription() {
    return description;
}
}

BUT, the Mushroom class/entity generated by jooq with my table Mushroom looks like:

@javax.annotation.Generated(value    = { "http://www.jooq.org", "3.2.0" },
                        comments = "This class is generated by jOOQ")
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Mushrooms extends org.jooq.impl.TableImpl<com.spanier.db.tables.records.MushroomsRecord> {

private static final long serialVersionUID = -539518621;

/**
 * The singleton instance of <code>public.mushrooms</code>
 */
public static final com.spanier.db.tables.Mushrooms MUSHROOMS = new com.spanier.db.tables.Mushrooms();

/**
 * The class holding records for this type
 */
@Override
public java.lang.Class<com.spanier.db.tables.records.MushroomsRecord> getRecordType() {
    return com.spanier.db.tables.records.MushroomsRecord.class;
}

/**
 * The column <code>public.mushrooms.mushrooms_id</code>. 
 */
public final org.jooq.TableField<com.spanier.db.tables.records.MushroomsRecord, java.lang.Integer> MUSHROOMS_ID = createField("mushrooms_id", org.jooq.impl.SQLDataType.INTEGER.nullable(false).defaulted(true), this);

/**
 * The column <code>public.mushrooms.name</code>. 
 */
public final org.jooq.TableField<com.spanier.db.tables.records.MushroomsRecord, java.lang.String> NAME = createField("name", org.jooq.impl.SQLDataType.CHAR.length(50).nullable(false), this);

/**
 * The column <code>public.mushrooms.description</code>. 
 */
public final org.jooq.TableField<com.spanier.db.tables.records.MushroomsRecord, java.lang.String> DESCRIPTION = createField("description", org.jooq.impl.SQLDataType.VARCHAR, this);

/**
 * Create a <code>public.mushrooms</code> table reference
 */
public Mushrooms() {
    super("mushrooms", com.spanier.db.Public.PUBLIC);
}

/**
 * Create an aliased <code>public.mushrooms</code> table reference
 */
public Mushrooms(java.lang.String alias) {
    super(alias, com.spanier.db.Public.PUBLIC, com.spanier.db.tables.Mushrooms.MUSHROOMS);
}

/**
 * {@inheritDoc}
 */
@Override
public org.jooq.Identity<com.spanier.db.tables.records.MushroomsRecord, java.lang.Integer> getIdentity() {
    return com.spanier.db.Keys.IDENTITY_MUSHROOMS;
}

/**
 * {@inheritDoc}
 */
@Override
public org.jooq.UniqueKey<com.spanier.db.tables.records.MushroomsRecord> getPrimaryKey() {
    return com.spanier.db.Keys.MUSHROOMS_PKEY;
}

/**
 * {@inheritDoc}
 */
@Override
public java.util.List<org.jooq.UniqueKey<com.spanier.db.tables.records.MushroomsRecord>> getKeys() {
    return java.util.Arrays.<org.jooq.UniqueKey<com.spanier.db.tables.records.MushroomsRecord>>asList(com.spanier.db.Keys.MUSHROOMS_PKEY);
}

/**
 * {@inheritDoc}
 */
@Override
public com.spanier.db.tables.Mushrooms as(java.lang.String alias) {
    return new com.spanier.db.tables.Mushrooms(alias);
}
}

As you can see the class generated by jooq is: 1. already filled with annotations, if I add new annotation it will be complicated to easily 'read' the class 2. since the jooq class is generated each time I make a modification in the Mushroom table I will lost my annotations (annotations for the json representation) and I will need to edit AGAIN the jooq class.

So Im looking for a way to add the Resteasy annotations (json representation) of the Mushroom generated class in another class/in another file.

Community
  • 1
  • 1
Jeremy S.
  • 125
  • 3
  • 9
  • What exactly *is* your problem? – Lukas Eder Nov 02 '13 at 18:43
  • Hi Lukas, my problem is that I don't want to add annotation to the class directly since the classes can be generated each time a change is made in the database. I can avoid this behavior obviously for jooq but I think that separate 'resteasy annotations' would be easier to manage in a team project. Moreover the classes generated by jooq are also full of annotations, complicated to read. – Jeremy S. Nov 03 '13 at 12:27
  • Jeremy, I think this question needs some code, showing what you have, and where you want to go. It is very difficult to help you at the current stage of this question... In other words, it is a bit difficult to read your mind :-) – Lukas Eder Nov 03 '13 at 14:10
  • I added code in the first post. thank you! – Jeremy S. Nov 30 '13 at 13:49

3 Answers3

0

I have done something similar to this with Jersey. You can use the mixin functionality to create an external interface and register the mixin with jersey. This was how I was able to work around adding things like serialization to external pojo's.

How can I tell jackson to ignore a property for which I don't have control over the source code?

BTW you would still need to create your resource classes with the @Path annotations though. I don't know a way around this. I am assuming you are trying to add serialization parameters to the pojo objects without adding annotations. In this case the mixin's is probably your most optimal solution.

Community
  • 1
  • 1
Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65
0

I can only see these options for you with jOOQ 3.2:

1. Write your own code-generator

This is partially documented in the manual's section about custom code sections. In your case, you might need to completely rewrite the generation of POJOs (or generate additional POJOs) to have them annotated with JAXB or other annotations

2. Write your POJOs manually to have more control over them

This might be a better option in the long run, if you want to fully control each JSON data structure. jOOQ's DefaultRecordMapper (as used by ResultQuery.fetchInto(Class), for instance) might still be able to map onto them

3. Write your POJOs manually and write your own RecordMapperProvider

If your JSON data structures are largely different than your database model, you might want to consider overriding most of jOOQ's default behaviour by writing your own RecordMapperProvider

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
0

First remark: you should always separate your persistent classes from your data transfert objects. This to enable your application to be applied on any database system without any impact on your web services contracts.

Second remark: Having real DTO enable you to either add your own annotations on these DTO classes, or just do not add annotations as your objects can be automatically converted into JSON format by specifying the data return type of your web services.

kij
  • 1,421
  • 1
  • 16
  • 40