spring Spring和jackson,如何通过@ResponseBody禁用FAIL_ON_EMPTY_BEANS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28862483/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:24:55  来源:igfitidea点击:

Spring and Hymanson, how to disable FAIL_ON_EMPTY_BEANS through @ResponseBody

springHymanson

提问by user2412555

Is there a global configuration in spring that can disable spring FAIL_ON_EMPTY_BEANS for all controller annotated with @ResponseBody?

spring 中是否有全局配置可以禁用所有用@ResponseBody 注释的控制器的 spring FAIL_ON_EMPTY_BEANS?

采纳答案by vtor

You can configure your object mapper when configuring configureMessageConverters

您可以在配置时配置您的对象映射器 configureMessageConverters

@Bean
public MappingHymanson2HttpMessageConverter mappingHymanson2HttpMessageConverter() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    MappingHymanson2HttpMessageConverter converter = 
        new MappingHymanson2HttpMessageConverter(mapper);
    return converter;
}

If you want to know how to do exactly in your application, please update your question with your configuration files (xml or java configs).

如果您想知道如何在您的应用程序中准确执行,请使用您的配置文件(xml 或 java 配置)更新您的问题。

Here is a good articlehow to customize message converters.

这是一篇关于如何自定义消息转换器的好文章

Edit:If you are using XML instead of Java configs, you can create a custom MyJsonMapperclass extending ObjectMapperwith custom configuration, and then use it as follows

编辑:如果您使用的是 XML 而不是 Java 配置,您可以创建一个使用自定义配置MyJsonMapper扩展的自定义类ObjectMapper,然后按如下方式使用它

public class MyJsonMapper extends ObjectMapper {    
        public MyJsonMapper() {
            this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        }
}

In your XML:

在您的 XML 中:

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
                <property name="objectMapper" ref="HymansonObjectMapper" />
            </bean>
        </mvc:message-converters>
</mvc:annotation-driven>


<bean id="HymansonObjectMapper" class="com.mycompany.example.MyJsonMapper" >

回答by user238607

If you are using Spring Boot, you can set the following property in application.properties file.

如果您使用的是 Spring Boot,您可以在 application.properties 文件中设置以下属性。

spring.Hymanson.serialization.FAIL_ON_EMPTY_BEANS=false

spring.Hymanson.serialization.FAIL_ON_EMPTY_BEANS=false

Thanks to @DKroot for his valuable comment. But I believe this should be its own answer for others.

感谢@DKroot 的宝贵意见。但我相信这应该是其他人自己的答案。

回答by wdavilaneto

If you are using Spring Boot / JPA , you also have to observe if you are using

如果您使用的是 Spring Boot / JPA ,您还必须观察您是否使用

getOne (goes for jpa getReference ) for the findOne / enetiyManager.find(Clazz , id)

getOne(用于 jpa getReference )用于 findOne / enetiyManager.find(Clazz , id)

GetOne Relies on Persistence cached reference by ID that is designed to retrieve entity with only ID in it. Its use was mostly for indicating reference existed without the need to retrieve whole entity.

GetOne 依赖于通过 ID 的 Persistence 缓存引用,该引用旨在检索其中只有 ID 的实体。它的用途主要是指示存在引用,而无需检索整个实体。

The find method is straight forward to persistence manager to obtain the persistent instance.

find 方法直接让持久化管理器获取持久化实例。

This second one will observe you annotation @JsonIgnore accordingly and will give you the expected result.

第二个将相应地观察您的注释 @JsonIgnore 并为您提供预期的结果。

 // On entity...
 @JsonIgnore
 @OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
 private List<Foo> fooCollection;

 // later on persistence impl
 entityManager.find(Caso.class, id);

 // or on serivce 
 casoRepository.findById(id); //...

回答by Evol Rof

cant find spring.Hymanson.serialization.FAIL_ON_EMPTY_BEANS=falsein spring boot 2.2.5

spring.Hymanson.serialization.FAIL_ON_EMPTY_BEANS=false在 spring boot 2.2.5 中找不到

I use this

我用这个

@Configuration
public class SerializationConfiguration {
    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }
}

回答by Gourab Bhattacharyya

For me, the Issue was with typecasting from org.json.JSONObjectobject to org.json.simple.JSONObjectand I solved it by parsing value from org.json.JSONObjectand then cast it to use as org.json.simple.JSONObject

对我来说,问题是从org.json.JSONObject对象到org.json.simple.JSONObject 的类型转换,我通过解析来自org.json.JSONObject 的值来解决它,然后将其转换为org.json.simple.JSONObject

JSONParser parser = new JSONParser();
org.json.simple.JSONObject xmlNodeObj = (org.json.simple.JSONObject) parser.parse(XMLRESPONSE.getJSONObject("xmlNode").toString());