我正在使用 JAX-RS 创建一个小型 Web 服务,但无法访问我的 GET 请求 http://localhost:8080/MyProject/resources/agenda/ {jour}
这是我的代码:
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 类:
包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;
}
}
我的数据库:
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;
}
}
我用这个类调用网络服务:
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;
}
}
如果我调用/agenda,它会返回我的所有 Activity 。 像这样:
但是,如果我调用/agenda/1 ,它应该返回我的第一个 Activity ...
在我的控制台中:getTrancheHoraire 返回如下内容:com.project.test.TrancheHoraire@75a630fb
我读过 Equals() 类上的插件是唯一的解决方案。
你能帮我吗? :)
最佳答案
"I've read plugin on Equals() class is the only one solution."
我猜“插件打开”意味着覆盖。如果不是,那么这就是你的意思。您需要覆盖它,并描述如何确定对象相等。 (还应该注意的是,当覆盖 equals
时,还应该覆盖 hashcode
)。
话虽这么说,大多数 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);
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;
}
我知道 Eclipse 有类似的功能。
顺便说一句,你的比较看起来有点奇怪。为什么需要创建一个新的Activite
?该方法是 getActiviteByDate
,那么为什么不直接查找带有日期的 Activites
呢?
关于java - JAX-RS/JAXB : equals(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30360866/