相关疑难解决方法(0)

在Spring中配置ObjectMapper

我的目标是配置objectMapper它只序列化带注释的元素@JsonProperty.

为了做到这一点,我按照这个解释说明了如何配置对象映射器.

我在这里描述了自定义objectmapper .

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

有人有提示吗?提前致谢

杰克逊1.8.0春季3.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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

NumbersOfNewEvents

public class …
Run Code Online (Sandbox Code Playgroud)

java spring json jackson object-object-mapping

76
推荐指数
8
解决办法
20万
查看次数

验证 Java LocalDate 是否与 yyyy-MM-dd 格式与可读消息匹配

我在 DoB 的 POJO 类中有以下属性。

@NotNull(message = "dateOfBirth is required")
@JsonDeserialize(using = LocalDateDeserializer.class)
LocalDate dateOfBirth;
Run Code Online (Sandbox Code Playgroud)

我怎样才能验证这一点

  1. 用户正在发送有效的日期格式(仅接受 YYYY-MM-DD)
  2. 如果用户输入不正确的日期,我想发送自定义消息或更易读的消息。目前,如果用户输入无效日期,则应用程序会发送以下长错误 -
JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String \"1984-33-12\": Failed to deserialize java.time.LocalDate:
(java.time.format.DateTimeParseException) Text '1984-33-12' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 33; 
...
Run Code Online (Sandbox Code Playgroud)

java jackson bean-validation jackson-databind

5
推荐指数
1
解决办法
2万
查看次数