gpt4 book ai didi

java - 如何在以下场景中为模型的特定字段选择自定义序列化器

转载 作者:行者123 更新时间:2023-11-30 06:41:05 25 4
gpt4 key购买 nike

下面是两个自定义日期序列化器:

package test;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class DateFormatter extends StdSerializer<Date> {

private SimpleDateFormat sampleFormatOne= new SimpleDateFormat("dd-MMM-yyyy");


public DateFormatter(Class<Date> t) {
super(t);
}

public DateFormatter() {
super(Date.class);
}

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException,
JsonGenerationException {
java.util.Date inputDate = new java.util.Date(date.getTime());

jsonGenerator.writeString(sampleFormatOne.format(inputDate));
}

}

还有另一个日期序列化器:

package test;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

public class DateFormatterTwo extends StdSerializer<Date> {

private SimpleDateFormat samoelFormatTwo= new SimpleDateFormat("dd-mm-yyyy");y");

public DateFormatterTwo (Class<Date> t) {
super(t);
}

public DateFormatterTwo () {
super(Date.class);
}

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException,
JsonGenerationException {
java.util.Date inputDate = new java.util.Date(date.getTime());

jsonGenerator.writeString(samoelFormatTwo.format(inputDate));
}

}

Spring配置如下:

<beans:bean id="customDate"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<beans:property name="serializers">
<beans:array>
<beans:bean class="test.DateFormatter" />
<beans:bean class="test.DateFormatterTwo" />
</beans:array>
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>


<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
<beans:ref bean="customDate" />
</beans:list>
</beans:property>
</beans:bean>

现在我需要做的是创建一个 JSON 对象,该对象基于 f irst or second formatter 进行日期序列化取决于字段值employeeType 的值。

通过当前的实现,我可以生成具有固定第一个格式的 JSON。 如何在运行时根据另一个字段值选择序列化器。 ?

以下是模型中的字段

private field String employeeType;

@JsonProperty("empJoiningDate")
@JsonSerialize(using=DateFormatter.class)
private Date empJoiningDate = null;

最佳答案

为了使用取决于 employeeType 的格式序列化 empJoiningDate 字段,您需要为Employee POJO 类型而不是 Date 字段。

按照这种方法,您将可以访问所有对象字段,并可以提供条件日期序列化逻辑。

Serializer 实现如下:

private static class EmployeeSerializer extends StdSerializer<Employee> {

private static final String TYPE_ONE = "SomeType";

private static final String TYPE_TWO = "SomeOtherType";

private SimpleDateFormat sampleFormatOne = /** format initialized **/;

private SimpleDateFormat sampelFormatTwo = /** format initialized **/;

protected EmployeeSerializer() {
super(Employee.class);
}

@Override
public void serialize(Employee employee, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeString(employee.getEmployeeType());
switch (employee.getEmployeeType()) {
case TYPE_ONE: jsonGenerator.writeString(sampleFormatOne.format(employee.getEmpJoiningDate()));
case TYPE_TWO: jsonGenerator.writeString(sampelFormatTwo.format(employee.getEmpJoiningDate()));
}
jsonGenerator.writeEndObject();
}
}

然后,您需要使用自定义序列化程序而不是实例日期字段来注释您的 Employee 值类:

@JsonSerialize(using = EmployeeSerializer.class)
public class Employee {
//...
}

关于java - 如何在以下场景中为模型的特定字段选择自定义序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44364493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com