2

As the title already status I'm running an Java RS Rest API. The GET method that is called successfully returns an object with the correct data. However, after returning the value something goes wrong resulting in a 500 page displayed in the browser.

The Get method looks like this:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Verplaatsing> getAll(@QueryParam("start") String sD, @QueryParam("end") String eD, @QueryParam("auth") String authCode) {
    /* First check the auth code */
    /* First check the auth code */
    if (authCode == null || !authCode.equals(service.getAuthcode(rightAll))) {
        throw new WebApplicationException("Invalid or no auth key provided");
    }


    List<Verplaatsing> verplaatsing = new ArrayList<>();

    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date start = dateFormat.parse(sD);
        Date end = dateFormat.parse(eD);
        verplaatsing = service.getAllVerplaatsing(start, end);
    } catch (Exception ex) {
        throw new WebApplicationException(Response.Status.EXPECTATION_FAILED);
    }

    return verplaatsing;
}

The Verplaatsing class returned by the REST service looks like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "verplaatsing")
@Entity (name = "VERPLAATSING")
@IdClass(VerplaatsingId.class)
public class Verplaatsing implements Serializable
{
    @ManyToOne @Id
    private Registratiekast registratiekast;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP) 
    @Id
    private Date verplaatsingDatum;

    private double positie;
    private double snelheid;
    private String edge;
    private String lane;

    protected Verplaatsing()
    {
    }

    public Verplaatsing(Registratiekast registratiekast, Date date, double positie, double snelheid, String edge, String lane)`enter code here`
    {
        this.registratiekast = registratiekast;
        this.verplaatsingDatum = date;
        this.positie = positie;
        this.snelheid = snelheid;
        this.edge = edge;
        this.lane = lane;
    }

    public Date getVerplaatsingDatum()
    {
        return verplaatsingDatum;
    }

    public String getEdge()
    {
        return edge;
    }

    public String getLane()
    {
        return lane;
    }

    public double getPositie()
    {
        return positie;
    }

    public Registratiekast getRegistratiekast()
    {
        return registratiekast;
    }

    public double getSnelheid()
    {
        return snelheid;
    }

    @Override
    public String toString() {
        return "verplaatsing [registratiekast=" + this.registratiekast + ", date=" + this.verplaatsingDatum + ", positie="
                + this.positie + ", snelheid=" + this.snelheid  + ", edge=" + this.edge + "]";
    }
}

The weird thing is that there are no log items after a failed GET request. Which makes debugging this issue near impossible. The one major change I made to the code is moving the Verplaatsing class to another .jar. However, I can't imagine that is causing this problem.

Edit:

The entire WebService code:

@Path("verplaatsing")
@Stateless
public class VerplaastingREST {

    private static String rightAll = "all";
    private static String rightSingle = "single";

    @Inject
    VerplaatsingService service;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Verplaatsing> getAll(@QueryParam("start") String sD, @QueryParam("end") String eD, @QueryParam("auth") String authCode) {
        /* First check the auth code */
        /* First check the auth code */
        if (authCode == null || !authCode.equals(service.getAuthcode(rightAll))) {
            throw new WebApplicationException("Invalid or no auth key provided");
        }


        List<Verplaatsing> verplaatsing = new ArrayList<>();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            Date start = dateFormat.parse(sD);
            Date end = dateFormat.parse(eD);
            verplaatsing = service.getAllVerplaatsing(start, end);
        } catch (Exception ex) {
            throw new WebApplicationException(Response.Status.EXPECTATION_FAILED);
        }

        return verplaatsing;
    }

    @GET
    @Path("{text}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Verplaatsing> getVerplaatsingen(@PathParam("text") String id, @QueryParam("start") String sD, @QueryParam("end") String eD, @QueryParam("auth") String authCode) {
        /* First check the auth code */
        if (authCode == null || (!authCode.equals(service.getAuthcode(rightAll)) && !authCode.equals(service.getAuthcode(rightSingle)))) {
            throw new WebApplicationException("Invalid or no auth key provided");
        }

        List<Verplaatsing> verplaatsing = new ArrayList<>();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            Date start = dateFormat.parse(sD);
            Date end = dateFormat.parse(eD);
            verplaatsing = service.getAllVerplaatsing(id ,start, end);
        } catch (ParseException ex) {
            throw new WebApplicationException(Response.Status.EXPECTATION_FAILED);
        }

        return verplaatsing;
    }

}
10
  • Check the HTTP header on your JAX-RS web service, I'm almost certain that the server is sending an incorrect code when the request is made (500) but still sending the data as if the correct code (200) was sent. Apr 3, 2014 at 20:16
  • The size of the response is about 1.4 KB while the list returned contains about 30,000 items. So it definitely isn't returning the date. Apr 3, 2014 at 20:25
  • Maybe you misunderstood what I said. I think the problem is not on the client side of the code, but on the server, where the JAX-RS service is posted. If you could post the code of the Web Service I could check if everything is working like it should. Apr 3, 2014 at 20:32
  • Oh, sorry. It's posted. Apr 3, 2014 at 20:35
  • Ok, now that I see the full code and the glassfish tag on the question you may need to check the server.log, located in the glassfish installation folder, normally under glassfish/domains/{yourDomain}/logs. My best guess at the error is the parsing of the JSON returned by the web service in your client code. How are you transforming the Verplaatsing objects into JSON? Apr 3, 2014 at 20:44

1 Answer 1

0

Alright, I found the solution. I manually added all the entities in the entity .jar to the .class list of the web service. In the ApplicationConfig.java file:

package org.netbeans.rest.application.config;

import com.s63c.verplaatsingdata.Registratiekast;
import com.s63c.verplaatsingdata.Verplaatsing;
import java.util.Set;
import javax.ws.rs.core.Application;

/**
 *
 * @author Koenkk
 */
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        resources.add(Verplaatsing.class);
        resources.add(Registratiekast.class);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(com.s63c.verplaatsingsysteem.rest.VerplaastingREST.class);
    }

}   

No idea why this suddenly works, since it should work out of the box. But meh.

1
  • Glad you managed to solve your problem. Remember to accept your own answer! Apr 4, 2014 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.