在Spring Hibernate项目的json杰克逊中,如何忽略"handler":{},"hibernateLazyInitializer":{}? [英] How to ignore "handler": {}, "hibernateLazyInitializer": {} in json jackson in Spring hibernate project?

查看:66
本文介绍了在Spring Hibernate项目的json杰克逊中,如何忽略"handler":{},"hibernateLazyInitializer":{}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有对象映射器的fastxml json,下面是我的代码:

I am using fasterxml json with object mapper and below is my code:

 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);
        }

我实现并使用代码的方式,我得到了期望的输出,但是json在某些json对象中具有2个随机的奇怪值,如下所示:

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:

"listing": {
          "listingId": 1,
          "name": "Business",
          "handler": {},
          "hibernateLazyInitializer": {}
        },
        "handler": {},
        "hibernateLazyInitializer": {}
      },

如何配置objectmappper忽略"handler":{},"hibernateLazyInitializer":{} 输出的json中的值?

How to configure objectmappper to ignore "handler": {}, "hibernateLazyInitializer": {} values from outputted json ?

我尝试了以下解决方案:

I tried solutions below:

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

但是它不起作用,输出仍然与我上面发布的相同.另外,我知道我可以通过使用 @JsonIgnoreProperties({"hibernateLazyInitializer","handler"})注释类来忽略json中的这些处理程序和hibernateLazyInitializer,但是有没有任何方法来全局配置json杰克逊对象映射器,这样它就永远不会在我输出的json中添加这些值?

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 jackson object mapper so that it never adds these values in my outputted json?

推荐答案

您可以尝试将Mixin添加到Object.class:

You could try adding a mixin to Object.class:

public ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixIn(Object.class, IgnoreHibernatePropertiesInJackson.class);
    return mapper;
}

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private abstract class IgnoreHibernatePropertiesInJackson{ }

这篇关于在Spring Hibernate项目的json杰克逊中,如何忽略"handler":{},"hibernateLazyInitializer":{}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆