gpt4 book ai didi

java - Jersey + Jackson JSON 日期格式序列化 - 如何更改格式或使用自定义 JacksonJsonProvider

转载 作者:IT老高 更新时间:2023-10-28 20:50:01 24 4
gpt4 key购买 nike

我正在使用 Jersey + Jackson 为我的应用程序提供 REST JSON 服务层。我遇到的问题是默认的日期序列化格式如下所示:

"CreationDate":1292236718456

起初我以为它是一个 UNIX 时间戳……但它太长了。我的客户端 JS 库在反序列化这种格式时遇到问题(它支持一堆不同的日期格式,但我想不支持这种格式)。我想更改格式,以便我的库可以使用它(例如 ISO)。我该怎么做...我找到了一段可以提供帮助的代码,但是...我应该把它放在哪里,因为我不控制 Jackson 序列化程序实例化(Jersey 可以)?

objectMapper.configure(
SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

我还为自定义 JacksonJsonProvider 找到了这段代码 - 问题是 .. 如何让我的所有 POJO 类都使用它?

@Provider
public class MessageBodyWriterJSON extends JacksonJsonProvider {

private static final String DF = "yyyy-MM-dd’T'HH:mm:ss.SSSZ";

@Override
public boolean isWriteable(Class arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
return super.isWriteable(arg0, arg1, arg2,
arg3);
}
@Override
public void writeTo(Object target, Class arg1, Type arg2, Annotation[] arg3,
MediaType arg4, MultivaluedMap arg5, OutputStream outputStream)
throws IOException, WebApplicationException {
SimpleDateFormat sdf=new SimpleDateFormat(DF);

ObjectMapper om = new ObjectMapper();
om.getDeserializationConfig().setDateFormat(sdf);
om.getSerializationConfig().setDateFormat(sdf);
try {
om.writeValue(outputStream, target);
} catch (JsonGenerationException e) {
throw e;
} catch (JsonMappingException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
}

最佳答案

我设法在 Resteasy“JAX-RS 方式”中做到这一点,因此它应该适用于像 Jersey 这样的每个兼容实现(最近在 JEE7 服务器 Wildfly 8 上成功测试,它只需要对 Jackson 部分进行一些更改,因为它们更改了一些 API)。

您必须定义一个 ContextResolver(检查 Produces 是否包含正确的内容类型):

import javax.ws.rs.ext.ContextResolver;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.DeserializationConfig;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.Produces;
import java.text.SimpleDateFormat;
@Provider
@Produces("application/json")
public class JacksonConfigurator implements ContextResolver<ObjectMapper> {

private ObjectMapper mapper = new ObjectMapper();

public JacksonConfigurator() {
SerializationConfig serConfig = mapper.getSerializationConfig();
serConfig.setDateFormat(new SimpleDateFormat(<my format>));
DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
deserializationConfig.setDateFormat(new SimpleDateFormat(<my format>));
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
}

@Override
public ObjectMapper getContext(Class<?> arg0) {
return mapper;
}

}

那么你必须在你的 javax.ws.rs.core.Application 的 getClasses 中返回新创建的类<​​/p>

import javax.ws.rs.core.Application;
public class RestApplication extends Application {

@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
// your classes here
classes.add(JacksonConfigurator.class);
return classes;
}

}

这样,通过 jackson 进行的所有操作都会被赋予您选择的 ObjectMapper。

编辑:我最近发现,如果您决定扩展 JacksonConfigurator 以添加自定义映射,则使用 RestEasy 2.0.1(以及 Jackson 1.5.3)会出现奇怪的行为。

import javax.ws.rs.core.MediaType;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class MyJacksonConfigurator extends JacksonConfigurator

如果您只是这样做(当然将扩展类放在 RestApplication 中),则使用父类的映射器,即您丢失了自定义映射。为了让它正常工作,我不得不做一些对我来说似乎没用的事情:

public class MyJacksonConfigurator extends JacksonConfigurator implements ContextResolver<ObjectMapper> 

关于java - Jersey + Jackson JSON 日期格式序列化 - 如何更改格式或使用自定义 JacksonJsonProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4428109/

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