robinwood13
robinwood13

Reputation: 199

rest jax-rs : server error

I'm creating a little webservice with JAX-RS and I cannot access to my GET request http://localhost:8080/MyProject/resources/agenda

I just want my list of my activities in XML format.

Here is my code

package com.project.test;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;


@XmlRootElement(name = "activite")
@XmlAccessorType(XmlAccessType.FIELD)

public class Activite
{

  @XmlElement(name="nomactivite")
  private String but;
  @XmlElement(name="tranchehoraire", type=TrancheHoraire.class)
  private TrancheHoraire trancheHoraire;
  @XmlElement(name="lieu")
  private String lieu;
  //------------------------------------------------------------------------
  public Activite(String but,TrancheHoraire trancheHoraire, String lieu)
  {
    this.but = but;
    this.trancheHoraire = trancheHoraire;
    this.lieu = lieu;
  }
  //------------------------------------------------------------------------
  public String         getBut()            { return but; }

  public String getLieu() { return lieu; }

  public TrancheHoraire getTrancheHoraire() { return trancheHoraire; }

  public void setBut(String but) { 
    this.but = but; 
  } 

  public void setTrancheHoraire(TrancheHoraire trancheHoraire) { 
    this.trancheHoraire = trancheHoraire; 
  } 

  public void setLieu(String lieu) { 
    this.lieu = lieu; 
  } 
}

My database :

package com.project.test;

import java.util.ArrayList;
import java.util.List;


public class ActiviteBD { 

    private static List<Activite> activites = new ArrayList<Activite>(); 

    static { 
        activites.add(new Activite("Réunion", new TrancheHoraire(new Date(01, 10, 2015), 2), "Paris")); 
        activites.add(new Activite("Vacances", new TrancheHoraire(new Date(02, 10, 2015), 2), "Marseille")); 
        activites.add(new Activite("Resto", new TrancheHoraire(new Date(03, 10, 2015), 2), "Lyon")); 
    } 

    public static List<Activite> getActivites() { 
        return activites; 
    }
} 

Agenda = ActiviteResource

package com.project.test;


import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


@Path("/agenda")
public class Agenda
{

    @GET
    @Produces("application/xml")
    public List<Activite> getActivites() { 
        System.out.println("getActivites"); 
        System.out.println(ActiviteBD.getActivites());
        return ActiviteBD.getActivites(); 
    } 

    @GET   
    @Path("/{jour}/{mois}/{annee}/{part_j}")
    @Produces("application/xml")
    public Activite getActiviteByDate(@PathParam("jour") int jour, @PathParam("mois") int mois, @PathParam("annee") int annee, @PathParam("part_j") int part_j) { 
        System.out.println("getActivite"); 
        TrancheHoraire tranche = new TrancheHoraire(new Date(jour, mois, annee), part_j);
        for (Activite current : ActiviteBD.getActivites()) { 
            if (tranche.equals(current.getTrancheHoraire())) {
                return current;
            } 
        } 
        return null; 
    } 
}

My Date class

package com.project.test;


public class Date
{

  private int jour,mois,annee;

  public Date(int jour, int mois, int annee)
  {

    this.jour   = jour;
    this.mois   = mois;
    this.annee  = annee;
  }


  public int getJour()   { return jour; }
  public int getMois()   { return mois; }
  public int getAnnee()  { return annee; }

  public String toString()
  {
    return
         jour + "/"
         + mois + "/"
         + annee;
  }

}

And My TrancheHoraire class

package com.project.test;

public class TrancheHoraire
{

  private Date date;
  private int part_journee;
  public String part_journee_v;

  public TrancheHoraire(Date date, int part_journee)
  {
    this.date = date;
    this.part_journee = part_journee;

        if (part_journee == 1){
            this.part_journee_v = "le matin";
        }
        else{
            if (part_journee == 2){
                this.part_journee_v = "l' apres-midi";
            }
            else
            {
                this.part_journee_v = "erreur";
            }
        }

  }

  public Date getDate() { return date; }

  public int  getpart_journee()
  {
      return part_journee;
  }


  @Override
  public String toString()
  {
    return ("Tranche horaire du " + date +" -> " + part_journee_v);
  }

}

For information, ApplicationConfig contains :

private void addRestResourceClasses(Set<Class<?>> resources) {
    resources.add(com.project.test.Agenda.class);
} 

And I created my Web Java Application with GlassFish, JAVA 6 on NetBeans.

When I add a "HelloWorld" GET method in "Agenda" it works. So I don't think the problem comes from settings.

In my console, when I call agenda :

[com.project.airbus.Activite@2d30af8c, com.project.airbus.Activite@7e5dac9c, com.project.airbus.Activite@da9eec7]

Thanks for all in advance

Upvotes: 2

Views: 144

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209082

The only problem I faced was this exception

...IllegalAnnotationsException... Activite does not have a no-arg default constructor.

To fix it I simply added a no-arg constructor in the Activite class.

Also, you may not face this problem with your GET request, but with POST request, when you try and send XML, JAXB will not be able to create your other classes either, because they don't have no-arg constructors. So you might as well add them to all your model classes.

Upvotes: 1

Related Questions