views:

26

answers:

1

I am having a really strange problem implementing Java REST service. I am trying receive the following model:

@Data @EqualsAndHashCode(callSuper=false, of={"primaryKey"}) 
@Entity @Table(name = "T_UNIQUE_IDENT_TYPE")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@XmlRootElement(name="UniqueIdentifierType") @XmlAccessorType(XmlAccessType.FIELD)
public class UniqueIdentifierType extends AbstractEntityWithTime implements Serializable {
    private static final long serialVersionUID = 1L;    
    @Id @Basic(optional = false) @Column(name = "F_TYPE_PK") @XmlTransient
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="S_UNIQUE_IDENT_TYPE") @SequenceGenerator(name="S_UNIQUE_IDENT_TYPE",sequenceName="S_UNIQUE_IDENT_TYPE") 
    private Long primaryKey;
    @Column(name = "F_NAME", nullable=false, length=50)
    private String typeName;
    @Basic(optional = false) @Column(name = "F_CREATION_TIME") @Temporal(TemporalType.TIMESTAMP) @XmlTransient
    private Calendar creationTime;
    @Basic(optional = false) @Column(name = "F_LASTMODIFY_TIME") @Temporal(TemporalType.TIMESTAMP) @XmlTransient
    private Calendar lastModifyTime;
    @JoinColumn(name = "F_UNIT_PK", referencedColumnName = "F_UNIT_PK") @ManyToOne(optional = false) @XmlTransient
    private SoftwareProviderUnit softwareProviderUnit;
}

My GET rest service has the following signature:

@GET 
@Path("/{name}") 
public UniqueIdentifierType get(@PathParam("unitId") final Long unitId, @PathParam("name") final String name) throws WebApplicationException {

Whenever I do a call on this GET service I receive 500 error containing

java.lang.AssertionError: JAXBException occurred : 2 counts of IllegalAnnotationExceptions

I was able to trace it down to the following two errors:

javassist.util.proxy.MethodHandler is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at javassist.util.proxy.MethodHandler
        at private javassist.util.proxy.MethodHandler com.rp.central.model.UniqueIdentifierType_$$_javassist_21.handler
        at com.rp.central.model.UniqueIdentifierType_$$_javassist_21

javassist.util.proxy.MethodHandler does not have a no-arg default constructor.
    this problem is related to the following location:
        at javassist.util.proxy.MethodHandler
        at private javassist.util.proxy.MethodHandler com.rp.central.model.UniqueIdentifierType_$$_javassist_21.handler
        at com.rp.central.model.UniqueIdentifierType_$$_javassist_21

The weird thing is that if I change the GET method signature to return a List I get 200 with the JSON view of the object which has all the fields except for the typeName field. I am guessing that the exception is somehow related to that field. I've tried adding explicit getters such as

@XmlElement(name="typeName")
public String getTypeName() {
    return typeName;
}

to try to force it to send the typeName, this gets the type name into the "List" version of the GET method but still returns 500 for the non list one. The strange thing is that I have another model which looks exactly the same as this one and works without any problems. I've been banging my head against this for a while now, any help will be greatly appreciated.

Thank you.

+1  A: 

The exception clearly says that JAXB can't marshal your UniqueIdentifierType, because the object being marshalled is actually an instance of the class enhanced by Javassist (com.rp.central.model.UniqueIdentifierType_$$_javassist_21), and that class contains a field (added by Javassist) which can't be marshalled. Probably, ehancement is done by your ORM tool.

The most obvious solution is to return a fresh copy of the object.

axtavt
Thank you, this worked! but why is this happening in the first place? I have other objects which are exactly the same and work just fine. I would really like to keep the code consistent.
Alex
@Alex: If your `UniqueIdentifierType` is loaded by ORM, it may be somehow related to lazy-loading configuration.
axtavt