JAX-RS / JAXB:equals() [英] JAX-RS / JAXB : equals()

查看:74
本文介绍了JAX-RS / JAXB:equals()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-RS创建一个小型Web服务,我无法访问我的GET请求


话虽如此,大多数IDE都可以为您生成。例如,使用Netbeans,我只需右键单击该类,选择插入代码并选择 equals()和hashcode()。然后选择我想要包含在比较中的属性。我选择了所有,并得到了这个

  @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);
返回哈希值;
}

@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;
}
返回true;
}

我知道Eclipse有类似功能。






顺便说一下,你的比较看起来很奇怪。为什么需要创建新的 Activite ?该方法是 getActiviteByDate ,那么为什么不用日期查找 Activites


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 :

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 ? :)

解决方案

"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.

这篇关于JAX-RS / JAXB:equals()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆