java 如何在 Spring hibernate 项目的 json jackson 中忽略“handler”:{}、“hibernateLazyInitializer”:{}?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41582979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ignore "handler": {}, "hibernateLazyInitializer": {} in json Hymanson in Spring hibernate project?
提问by
I am using fasterxml json with object mapper and below is my code:
我正在使用带有对象映射器的 fastxml json,下面是我的代码:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
String jsonInString = mapper.writeValueAsString(myClassObjectHere);
return new ResponseEntity<String>(jsonInString, HttpStatus.OK);
} catch (JsonProcessingException e) {
e.printStackTrace();
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
The way I have implemented and am using the code, i get desired output but json has 2 random strange values in certain json objects as follows:
我实现和使用代码的方式,我得到了所需的输出,但 json 在某些 json 对象中有 2 个随机的奇怪值,如下所示:
"listing": {
"listingId": 1,
"name": "Business",
"handler": {},
"hibernateLazyInitializer": {}
},
"handler": {},
"hibernateLazyInitializer": {}
},
How to configure objectmappper to ignore "handler": {}, "hibernateLazyInitializer": {}
values from outputted json ?
如何配置 objectmappper 以忽略"handler": {}, "hibernateLazyInitializer": {}
来自输出 json 的值?
I tried solutions below:
我尝试了以下解决方案:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
But its not working and the output is still the same as i posted above.
Also, I know I can ignore these handler and hibernateLazyInitializer in json by annotating classes with @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
but is there any way to globally configure json Hymanson object mapperso that it never adds these values in my outputted json?
但它不起作用,输出仍然与我上面发布的相同。另外,我知道我可以通过注释类来忽略 json 中的这些处理程序和 hibernateLazyInitializer,@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
但是有没有办法全局配置 json Hymanson 对象映射器,以便它永远不会在我输出的 json 中添加这些值?
回答by alarive
You could try adding a mixin to Object.class:
您可以尝试在 Object.class 中添加一个 mixin:
public ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Object.class, IgnoreHibernatePropertiesInHymanson.class);
return mapper;
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private abstract class IgnoreHibernatePropertiesInHymanson{ }
回答by zidjian
I just ran into the same problem and found a way to globallyget hibernate entities mapped correctly by adding the Hibernate4Module to the mapper:
我刚刚遇到了同样的问题,并找到了一种通过将 Hibernate4Module 添加到映射器来全局正确映射休眠实体的方法:
objectMapper.registerModule(
new Hibernate4Module().configure(
Hibernate4Module.Feature.FORCE_LAZY_LOADING, true));
回答by shazin
Seems like "handler": {}, "hibernateLazyInitializer": {}
properties seems to be empty or null. You can use the following configuration to ignore empty or null properties.
似乎"handler": {}, "hibernateLazyInitializer": {}
属性似乎为空或为空。您可以使用以下配置忽略空或空属性。
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Or if you have access to the Class annotate handler
and hibernateLazyInitializer
with @JsonIgnore
this will prevent it from being Serialized globally.
或者,如果您有权访问类注释handler
,hibernateLazyInitializer
并且@JsonIgnore
这将阻止它在全局范围内被序列化。