[英]How to prevent json output of relTargetType in spring-data-rest?
I'm creating a @RepositoryRestResource
and export it as rest service as follows: 我正在创建一个@RepositoryRestResource
并将其导出为rest服务,如下所示:
@RepositoryRestResource(collectionResourceRel = "myContent", path = "myContent")
public interface MyContentRepository extends PagingAndSortingRepository<MyContentEntity, Long> {
}
Problem: when I request the content, I'm getting the following excerpt: 问题:当我请求内容时,我得到以下摘录:
"content" : [ {
"value" : [ ],
"rel" : null,
"collectionValue" : true,
"relTargetType" : "com.domain.MyContentEntity"
} ],
Question: how can I prevent to expose the relTargetType
package and "real" domain name? 问题:如何防止暴露relTargetType
包和“真实”域名?
If you don't want relTargetType in the JSON at all: 如果您根本不想在JSON中使用relTargetType:
@JsonIgnore
public String getRelTargetType() {
return relTargetType;
}
If you just want to hide the package: 如果您只想隐藏包裹:
public String getRelTargetType() {
return relTargetType.split("\\.")[2];
}
If you want to hide the package and return a different domain name: 如果要隐藏包并返回其他域名:
public String getRelTargetType() {
return "AlteredDomainName";
}
I'm not familiar with Spring Rest Data, but as far as I can tell it uses Jackson for its JSON handling. 我不熟悉Spring Rest Data,但据我所知,它使用Jackson进行JSON处理。
If that is indeed the case, I would suggest that your situation calls for the use of Mix-in annotations , which are intended for controlling the serialization of classes that cannot be modified. 如果确实如此,我建议您的情况要求使用Mix-in注释 ,这些注释用于控制无法修改的类的序列化。
First create a simple mix-in class with the JsonIgnoreType
annotation set. 首先使用JsonIgnoreType
注释集创建一个简单的混合类。
@JsonIgnoreType
public class OmitType {}
Next up, register the mix-in in the ObjectMapper
instance used. 接下来,在使用的ObjectMapper
实例中注册混合。 As far as I can tell, you get access to it in Spring Rest Data by following these instructions : 据我所知,您可以按照以下说明在Spring Rest Data中访问它:
To add your own Jackson configuration to the ObjectMapper used by Spring Data REST, override the configureJacksonObjectMapper method. 要将自己的Jackson配置添加到Spring Data REST使用的ObjectMapper中,请覆盖configureJacksonObjectMapper方法。 That method will be passed an ObjectMapper [...] 该方法将传递给ObjectMapper [...]
In the configureJacksonObjectMapper
method, register the mix-in with the unwanted type like this: 在configureJacksonObjectMapper
方法中,使用不需要的类型注册混合,如下所示:
objectmapper.addMixIn(RelTargetType.class, OmitType.class);
Note that RelTargetType.class
is just a guess. 请注意, RelTargetType.class
只是猜测。 Change to whatever type the field actually contains. 更改为字段实际包含的任何类型。 This should make Jackson ignore fields of that specific type whenever it encounters them. 这应该使Jackson在遇到它时忽略该特定类型的字段。
Added: 添加:
In case the relTargetType
field in MyContentEntity
is actually just a string field, the mix-in could be changed to the following instead: 万一relTargetType
现场MyContentEntity
实际上只是一个字符串字段,将混入可以改变,而不是以下内容:
public abstract class OmitType {
@JsonIgnore
public abstract String getRelTargetType();
}
And registering it should be changed to: 注册它应该改为:
objectmapper.addMixIn(MyContentEntity.class, OmitType.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.