休息 jax-rs:服务器错误

rest jax-rs : server error

我正在使用 JAX-RS 创建一个小网络服务,但我无法访问我的 GET 请求 http://localhost:8080/MyProject/resources/agenda

我只想要 XML 格式的我的活动列表。

这是我的代码

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

我的数据库:

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

议程 = 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; 
    } 
}

我的约会对象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;
  }

}

还有我的 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);
  }

}

有关信息,ApplicationConfig 包含:

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

然后我使用 GlassFish 创建了我的 Web Java 应用程序,JAVA 6 在 NetBeans 上。

当我在 "Agenda" 中添加 "HelloWorld" GET 方法时,它起作用了。 所以我不认为问题出在设置上。

在我的控制台中,当我调用议程时:

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

提前感谢大家

我遇到的唯一问题是这个异常

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

为了修复它,我只是在 Activite class 中添加了一个无参数构造函数。

此外,您的 GET 请求可能不会遇到这个问题,但是对于 POST 请求,当您尝试发送 XML 时,JAXB 将无法创建您的其他 class 也是,因为它们没有无参数构造函数。所以你不妨将它们添加到你所有的模型中 classes.