robinwood13
robinwood13

Reputation: 199

JAX-RS / JAXB : equals()

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

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.*;

@XmlRootElement(name = "activite")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"but","trancheHoraire", "lieu"})
public class Activite
{
  //-----------------------------------------------------------------------------
//  @XmlElement(name="nomactivite")
  private String but;
  private TrancheHoraire trancheHoraire;
  private String lieu;
  //-----------------------------------------------------------------------------

  public Activite(){

  }

  public Activite(String but,TrancheHoraire trancheHoraire, String lieu)
  {
    this.but = but;
    this.trancheHoraire = trancheHoraire;
    this.lieu = lieu;
  }
  //-----------------------------------------------------------------------------
  public String         getBut()            { return but; }


  public TrancheHoraire getTrancheHoraire() {
      return trancheHoraire;
  }

  public String getLieu() { return lieu; }

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

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

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

  public Date getDate (){

      return this.getTrancheHoraire().getDate();
  }
}

TrancheHoraire class :

package com.project.test;

import javax.xml.bind.annotation.*;


@XmlAccessorType(XmlAccessType.FIELD)
//@XmlType(name = "trancheHoraire", propOrder = {"date", "part_journee"})
public class TrancheHoraire
{
  //-----------------------------------------------------------------------------
//  @XmlElement(required = true)
  private Date date;
//  @XmlElement(required = true)
  private int part_journee;
  public String part_journee_v;
  //-----------------------------------------------------------------------------
  public TrancheHoraire(){

  }

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

  }



  //-----------------------------------------------------------------------------
  public Date getDate() { return date; }

  public int  getpart_journee()
  {
      return part_journee;
  }

  }

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

And I call webservices with this class :

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;

/**
 *
 * @author rcaboni
 */
@Path("/agenda")
public class Resource
{


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


    @GET   
    @Path("{jour}")
    @Produces("application/xml")

      public Activite getActiviteByDate(@PathParam("jour") int jour){  
        System.out.println("getActivite"); 
        Activite tranche = new Activite("Réunion", new TrancheHoraire(new Date(jour, 10, 2015), 2), "Marseille");
        TrancheHoraire th = tranche.getTrancheHoraire();
        System.out.println(tranche.getDate());

        for (Activite _current : ActiviteBD.getActivites()) { 
            System.out.println(_current.getTrancheHoraire());
            if (th.equals(_current.getTrancheHoraire())) {
                System.out.println(_current.getTrancheHoraire());
                return _current;
            } 
        } 
        return null; 
    } 
}

If I call /agenda, it returns all my activities. Like this :

XML/GET

However, if I call /agenda/1 , it should return my first activitie...

In my console : getTrancheHoraire returns something like this : com.project.test.TrancheHoraire@75a630fb

I've read plugin on Equals() class is the only one solution.

Could you help me ? :)

Upvotes: 2

Views: 282

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209082

"I've read plugin on Equals() class is the only one solution."

I guess "plugin on" means override. If not, then that's you it should mean. You need to override it, and describe how the objects will be determined equal. (It should also be noted, when override equals, you should also override hashcode).

That being said, most IDEs, will be able to generate this for you. For example, with Netbeans, I just right click the class, select "Insert Code" and select equals() and hashcode(). Then select the properties I want to include in the comparison. I selected all, and got this

@Override
public int hashCode() {
    int hash = 5;
    hash = 79 * hash + Objects.hashCode(this.date);
    hash = 79 * hash + this.part_journee;
    hash = 79 * hash + Objects.hashCode(this.part_journee_v);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final TrancheHoraire other = (TrancheHoraire) obj;
    if (!Objects.equals(this.date, other.date)) {
        return false;
    }
    if (this.part_journee != other.part_journee) {
        return false;
    }
    if (!Objects.equals(this.part_journee_v, other.part_journee_v)) {
        return false;
    }
    return true;
}

I know Eclipse has similar feature.


As an aside, your comparison looks kind of odd. Why do you need to create a new Activite? The method is getActiviteByDate, so why don't you just look for Activites with the date.

Upvotes: 1

Chris
Chris

Reputation: 68

Try adding a / before your {jour} declaration in the @Path annotation, like so:

@Path("/{jour}")

The mapping you've got currently looks like it may be routing requests to /agenda1.

Upvotes: 1

Related Questions