0

I'm using jersey to create a json/xml REST api. But I've encountered some strange behavior in Moxy.

It seems to cut off a java long and round the value up when it is larger than a certain value.

The primary key I use for the entity in question is: 871687120000007010, but if I query my api to test, the following happens:

https://i.stack.imgur.com/QbExD.png

Note that the image shows the "EAN" (the primary key) has been cut off. After doing some testing with it I found out the following:

Using 9223372036854775807 as primary key (max value for 64bit signed integer)
Yields: 9223372036854776000 after it has been parsed by moxy. This is higher than a 64bit signed int can be.

But putting in 9223372036854774807
Yields 9223372036854775000

It seems to round high values up with 1000 precision.

Does anyone have an idea what is going on here ?

Model class:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "CONNECTION")
@XmlRootElement
public class P4Connection {

    @XmlElement
    @Column(name = "SENDER", nullable = false)
    private long sender;

    @XmlElement
    @Column(name = "RECEIVER", nullable = false)
    private long receiver;

    @Id
    @XmlElement(type = Long.class)
    @Column(name = "ID", nullable = false)
    private long ean;

    @XmlElement
    @Column(name = "LAST_COLLECT")
    private Date lastCollect;

    @ManyToMany
    private Set<Request> REQUEST;

    public P4Connection() {
        REQUEST = new HashSet<>();
    }

    @XmlTransient
    public long getSender() {
        return sender;
    }

    public void setSender(long sender) {
        this.sender = sender;
    }

    @XmlTransient
    public long getReceiver() {
        return receiver;
    }

    public void setReceiver(long receiver) {
        this.receiver = receiver;
    }

    @XmlTransient
    public long getEan() {
        return ean;
    }

    public void setEan(long id) {
        this.ean = id;
    }

    @XmlTransient
    public Date getLastCollect() {
        return lastCollect;
    }

    public void setLastCollect(Date lastCollect) {
        this.lastCollect = lastCollect;
    }

    public Set<Request> getRequests() {
        return REQUEST;
    }
}

The API method:

@GET
@Path("/{ean}")
@Produces(MediaType.APPLICATION_JSON)
public P4Connection getConnection(@PathParam("ean") String ean,
                                  @Context UriInfo uriInfo) throws AppException {
    long eancode = parseEAN(ean, uriInfo);
    Session session = Database.getInstance().getSession();
    Query query = session.createQuery("from P4Connection where ean = ?");
    query.setLong(0, eancode);
    List connections = query.list();
    session.close();

    if (connections.size() != 1)
        throw new AppException(ErrorCode.NOT_FOUND, uriInfo);
    System.out.println(((P4Connection) connections.get(0)).getEan());
    return (P4Connection) connections.get(0);
}

This doesn't happen when I render it as XML by changing the @Produces annotation

1 Answers1

0

Turns out the plugin I was using in my browser was incorrectly displaying the long value