JAX-RS / JAXB:等于()
JAX-RS / JAXB : equals()
我正在使用 JAX-RS 创建一个小网络服务,但我无法访问我的 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 class :
包 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;
}
}
我用这个 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;
}
}
如果我调用 /agenda,它 return 就是我所有的活动。
像这样:
但是,如果我调用 /agenda/1 ,它应该 return 我的第一个活动...
在我的控制台中:getTrancheHoraire returns 是这样的:com.project.test.TrancheHoraire@75a630fb
我读过关于 Equals() 的插件 class 是唯一的解决方案。
你能帮帮我吗? :)
尝试在 @Path 注释中的 {jour} 声明之前添加一个 /,如下所示:
@Path("/{jour}")
您当前获得的映射看起来可能正在将请求路由到 /agenda1。
"I've read plugin on Equals() class is the only one solution."
我想 "plugin on" 意味着覆盖。如果不是,那就是你应该的意思。您需要覆盖它,并描述如何确定对象相等。 (还需要注意的是,override equals
的时候,也要override hashcode
)。
- 见when should I override Equals function?
也就是说,大多数 IDE 都能够为您生成它。例如,对于 Netbeans,我只需右键单击 class、select "Insert Code" 和 select equals() and hashcode()
。然后 select 我想包含在比较中的属性。我 select 都读完了,得到了这个
@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
呢。
我正在使用 JAX-RS 创建一个小网络服务,但我无法访问我的 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 class :
包 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;
}
}
我用这个 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;
}
}
如果我调用 /agenda,它 return 就是我所有的活动。 像这样:
但是,如果我调用 /agenda/1 ,它应该 return 我的第一个活动...
在我的控制台中:getTrancheHoraire returns 是这样的:com.project.test.TrancheHoraire@75a630fb
我读过关于 Equals() 的插件 class 是唯一的解决方案。
你能帮帮我吗? :)
尝试在 @Path 注释中的 {jour} 声明之前添加一个 /,如下所示:
@Path("/{jour}")
您当前获得的映射看起来可能正在将请求路由到 /agenda1。
"I've read plugin on Equals() class is the only one solution."
我想 "plugin on" 意味着覆盖。如果不是,那就是你应该的意思。您需要覆盖它,并描述如何确定对象相等。 (还需要注意的是,override equals
的时候,也要override hashcode
)。
- 见when should I override Equals function?
也就是说,大多数 IDE 都能够为您生成它。例如,对于 Netbeans,我只需右键单击 class、select "Insert Code" 和 select equals() and hashcode()
。然后 select 我想包含在比较中的属性。我 select 都读完了,得到了这个
@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
呢。