我使用 Spring Data REST 的 projections功能以便在 JSON 中包含一些嵌套类型的对象:
{
"id": 1,
"name": "TEST",
"user": {
"id": 1,
"name": "user1"
},
_links: {
self: {
href: "http://localhost:8082/accounts/1{?projection}",
templated: true
},
user: {
href: "http://localhost:8082/accounts/1/users"
},
}
}
如何在嵌套对象中生成链接?我想要以下 JSON 表示形式:
{
"id": 1,
"name": "TEST",
"user": {
"id": 1,
"name": "user1",
_links: {
self: {
href: "http://localhost:8082/users/1",
templated: true
},
}
},
_links: {
self: {
href: "http://localhost:8082/accounts/1{?projection}",
templated: true
},
user: {
href: "http://localhost:8082/accounts/1/users"
},
}
}
附言我看到了this question ,但不知道如何在我的案例中使用它(如果可能的话)
最佳答案
我偶然发现了这个问题,正在寻找解决方案。在摆弄 Spring Data REST docs section on Excerpts 之后我已经找到了如何实现这一目标。
我假设 Account 是您的根对象,您希望它有一个嵌套的 Users 集合,其中每个用户依次有 _links .
1:为 Users 对象添加一个 Excerpt(无论如何,这是一种隐藏列表集合中不重要细节的便捷技术)
@Projection(name = "userExcerpt", types = { User.class })
public interface UserExcerpt {
String getName();
String getEmail();
...
}
2:将 Excerpt 与您的 UserRepository
@RepositoryRestResource(excerptProjection = UserExcerpt.class)
public abstract interface UserRepository extends JpaRepository<User, Long> ...
3:为Account添加一个Projection:
@Projection(types = {Account.class})
public interface AccountUsersProjection {
String getName();
...
Set<UserExcerpt> getUsers();
}
此处重要的一点是您的投影需要引用 UserExcerpt 而不是 User。这样,从 GET/accounts/projection=accountUsersProjection 返回的结果将如下所示:
{
"_embedded" : {
"accounts" : [ {
"name" : "ProjA",
"users" : [ {
"name" : "Customer Admin",
"email" : "customer@meshcloud.io",
"_links" : {
"self" : {
"href" : "http://localhost:8080/users/2{?projection}",
"templated" : true
}, ...
}
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/accounts/1"
},
...
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/accounts"
},
...
},
"page" : {
"size" : 50,
"totalElements" : 2,
"totalPages" : 1,
"number" : 0
}
}
关于java - 将链接添加到投影的嵌套对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327429/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象