我尝试了一些在搜索时发现的方法,但没有任何帮助,或者我没有正确实现。
我得到的错误
Direct self-reference leading to cycle (through reference chain: io.test.entity.bone.Special["appInstance"]->io.test.entity.platform.ApplicationInstance["appInstance"])
它们都扩展了基础实体,并且在基础(父类(super class))中它也有一个 appInstance。
基本实体看起来与此类似
@MappedSuperclass
public abstract class BaseEntity implements Comparable, Serializable {
@ManyToOne
protected ApplicationInstance appInstance;
//getter & setter
}
应用实体看起来像这样
public class ApplicationInstance extends BaseEntity implements Serializable {
private List<User> users;
// some other properties (would all have the same base and application instance . User entity will look similar to the Special.)
}
特殊实体
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType")
@JsonIgnoreProperties({"createdBy", "appInstance", "lastUpdatedBy"})
public class Special extends BaseEntity implements Serializable {
@NotNull
@Column(nullable = false)
private String name;
@Column(length = Short.MAX_VALUE)
private String description;
@NotNull
@Column(nullable = false)
private Double price;
@OneToOne
private Attachment image;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = SpecialTag.class)
@CollectionTable(name = "special_tags")
@Column(name = "specialtag")
private List<SpecialTag> specialTags;
@Temporal(TemporalType.TIME)
private Date specialStartTime;
@Temporal(TemporalType.TIME)
private Date specialEndTime;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = WeekDay.class)
@CollectionTable(name = "available_week_days")
@Column(name = "weekday")
private List<WeekDay> availableWeekDays;
@OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
private List<SpecialStatus> statuses;
@OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
private List<SpecialReview> specialReviews;
@Transient
private Integer viewed;
private Boolean launched;
@OneToMany(mappedBy = "special")
private List<CampaignSpecial> specialCampaigns;
@Override
@JsonIgnore
public ApplicationInstance getAppInstance() {
return super.getAppInstance();
}
}
Special 中的所有实体都继承自包含 AppInstance 的 BaseEntity
那我有办法得到特殊的
@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public Special findByGuestRef(@PathParam("ref") String pRefeference) {
// find the special and return it
return special;
}
在特殊实体上我尝试了以下操作
上面的链接
这些解决方案都不起作用。我做错了什么吗?
注意:是否也可以只编辑特殊的,因为其他实体在不同的包中并且不想编辑它们。
最佳答案
通常在响应中排除属性就像在它们的 getter 中添加一个 @JsonIgnore 注解一样简单,但是如果你不想在父类中添加这个注解,你可以覆盖 getter然后在上面添加注解:
public class Special extends BaseEntity implements Serializable {
...
@JsonIgnore
public ApplicationInstance getAppInstance() {
return this.appInstance;
}
...
}
注意:由于有多个框架,请确保您使用的是正确的 @JsonIgnore 注释,否则它将被忽略,请参阅 this answer例如。
另一个选项,更“手动”,只是为响应创建一个 bean,它将是特殊实例的一个子集:
@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public SpecialDTO findByGuestRef(@PathParam("ref") String pRefeference) {
// find the special and return it
return new SpecialDTO(special);
}
public class SpecialDTO {
//declare here only the attributes that you want in your response
public SpecialDTO(Special sp) {
this.attr=sp.attr; // populate the needed attributes
}
}
关于java - 直接自引用导致循环父类(super class)问题 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45714483/
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub