当前位置: 首页 > 面试题库 >

Java在Spring中配置ObjectMapper

丰辰沛
2023-03-14
问题内容

我的目标是配置objectMapper,使其仅序列化带有注释的元素@JsonProperty

为了做到这一点,我按照下面的说明进行了说明,该说明说明了如何配置对象映射器。

我包括自定义描述objectmapper这里。

但是,当类NumbersOfNewEvents被序列化时,它仍包含json中的所有属性。

有人暗示吗?提前致谢

杰克逊1.8.0Spring3.0.5

CustomObjectMapper

public class CompanyObjectMapper extends ObjectMapper {
    public CompanyObjectMapper() {
        super();
        setVisibilityChecker(getSerializationConfig()
                .getDefaultVisibilityChecker()
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
                .withFieldVisibility(JsonAutoDetect.Visibility.NONE)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
    }
}

servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="de.Company.backend.web" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="jacksonObjectMapper" />
                </bean>
            </list>
        </property>
    </bean>

    <bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" />
</beans>
NumbersOfNewEvents

public class NumbersOfNewEvents implements StatusAttribute {

    public Integer newAccepts;
    public Integer openRequests;

    public NumbersOfNewEvents() {
        super();
    }
}

问题答案:

使用Spring Boot(1.2.4)和Jackson(2.4.6),以下基于注释的配置对我有用。

@Configuration
public class JacksonConfiguration {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);

        return mapper;
    }
}


 类似资料:
  • 问题内容: 我的目标是配置,使其仅序列化带有注释的元素。 为了做到这一点,我按照下面的说明进行了说明,该说明说明了如何配置对象映射器。 我包括自定义描述objectmapper 这里。 但是,当类被序列化时,它仍包含json中的所有属性。 有人暗示吗?提前致谢 Jackson 1.8.0 spring 3.0.5 CustomObjectMapper servlet.xml NumbersOfNe

  • 这个问题是关于Spring中的Java config。是否可以用Java代码替换下面的声明。 其中:secure-app-context.xml

  • 问题内容: 是否可以在用Java编写的Spring配置中使用Spring的注释? 例如: 显然,不能直接实例化DataSource接口,但是为了简化起见,我在这里直接实例化了它。当前,当我尝试上述操作时,数据源对象仍然为null,并且Spring不会对其进行自动接线。 我通过返回一个Hibernate 对象成功地工作了。 所以我的问题特别是:是否有办法针对a ?或更笼统地说,在Spring Jav

  • 所以最近我将Spring配置从XML迁移到Java配置。这是一个Spring OAUTH2服务器,有些endpoint通过客户端身份验证得到保护,有些endpoint(confirm_access)通过用户身份验证得到保护,用户身份验证通过从筛选器(“AuthenticationFilter”)进行重定向委托给登录应用程序。但我无法使用Spring Security Java配置: 以下是我的Ja

  • 我正在学习一些Spring Security教程,并试图在不使用xml的情况下实现它们,但我似乎找不到任何关于替换默认UsernamePasswordAuthenticationFilter的内容。 与此问题类似,我想从登录表单中检索一个额外的参数。我遇到困难的地方是: 为了正确设置它,我需要从AuthenticationManagerBuilder开始构建吗?还是我错过了什么?

  • 为Ant[pattern='/session']添加web访问控制表达式“has role('role_admin')' 正在为org.springframework.security.web.util.matcher.anyrequestmatcher@1添加web访问控制表达式“anonymous”,已验证配置属性 验证的配置属性 检查请求的匹配:“/API/Member/Account”;针