La méthode Java RS Rest API renvoie avec succès la valeur, le code d'état est 500 et aucune exception dans le journal du serveur


En tant que titre déjà status, j'exécute une API Rest Java RS. La méthode GET appelée renvoie avec succès un objet avec les données correctes. Cependant, après avoir renvoyé la valeur, quelque chose ne va pas, ce qui entraîne une page 500 affichée dans le navigateur.

La méthode Get ressemble à ceci:

@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;
}

La classe Verplaatsing renvoyée par le service REST ressemble à ceci:

@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 + "]";
    }
}

La chose étrange est qu'il n'y a pas d'éléments de journal après l'échec d'une demande GET. Ce qui rend débogage de ce problème presque impossible. Le seul changement majeur que j'ai apporté au code est de déplacer la classe Verplaatsing vers une autre .pot. Cependant, je ne peux pas imaginer que cela cause ce problème.

Modifier:

Le code complet du WebService:

@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;
    }

}
Author: Cœur, 2014-04-03

1 answers

D'accord, j'ai trouvé la solution. J'ai ajouté manuellement toutes les entités de l'entité .pot à la .liste des classes du service Web. Dans l'ApplicationConfig.fichier java:

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);
    }

}   

Aucune idée pourquoi cela fonctionne soudainement, car cela devrait fonctionner hors de la boîte. Mais meh.

 0
Author: TheDutchDevil, 2014-04-04 07:51:14