【问题标题】:jackson jersey json : serializing date from java to jsonjackson jersey json:将日期从 java 序列化为 json
【发布时间】:2015-04-22 15:57:08
【问题描述】:

我的 JSON 输出如下所示:

 "company":{"companyId": 0, "companyName": "OnTarget Technologies", "companyTypeId": 0, "address": null,…},
    "projectParentId": 24,
    "projectAddress":{"addressId": 26, "address1": "4750 59th street", "address2": "Apt 9C", "city": "Woodside",…},
    "taskList":[{"projectTaskId": 9, "title": "Installation of Lights", "description": "Installation of lights",…],
    "projects": null,
    "startDate": 1424322000000,
    "endDate": 1427515200000,
    "projectImagePath": null
    },
    {"projectId": 26, "projectName"

数据库中startDate和endDate的数据类型是Datetime

我在 json 中作为长整数序列化时获取日期时间。

如何在序列化时将其转换为可读格式 格式 MM-dd-yyyy HH:mm:ss

我创建了一个提供程序,但它不起作用

这是我的提供者:

@Component
@Provider
public class MyObjectMapper implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper;

    public MyObjectMapper() {
        this.mapper = createObjectMapper();
    }

    private static ObjectMapper createObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
        mapper.setDateFormat(new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"));
        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
        return mapper;
    }

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

我正在使用jacskon jersey 2.x,spring mysql数据库

任何想法都值得赞赏。

谢谢 桑吉夫

【问题讨论】:

  • 你用的是哪个版本的jackson?
  • 怎么样:从对象映射中获取长值,但在插入数据库之前转换为日期
  • 我使用的是杰克逊 2.4.3。
  • @EfeKahraman 这是在将 JSON 发送回客户端期间。

标签: java json jersey jackson


【解决方案1】:

使用 jackson 2.4(ObjectMapper 的配置方式略有不同)你有正确的配置(见下文)

您确定正在使用您的提供程序中配置的对象映射器吗?

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class FooTest {

    @Test
    public void foo() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        mapper.setDateFormat(new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"));
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

        final String output = mapper.writeValueAsString(new Bar(new Date(10000000L)));
        assertThat(output, containsString("01-01-1970 03:46:40"));
    }

    private static class Bar {
        @JsonProperty("date")
        private Date date;

        public Bar() {
        }

        public Bar(Date date) {
            this.date = date;
        }
    }
}

【讨论】:

  • 我也有这个:com.fasterxml.jackson.jaxrsjackson-jaxrs-json-provider2.4.3 在我的 pom.xml 中。这会导致任何问题吗?
  • 奇怪;我的答案中的 ObjectMapper 配置适用于 jackson 2,但您的提供程序中的配置样式似乎适用于 jackson 1。您使用的是哪个版本的 jackson-databind?
  • 它的 2.4.3 。顺便说一句,使用 org.codehaus.* 与 com.fasterxml.* 的区别是什么。我的代码同时编译,但您的示例使用的是 com.fasterxml.*
  • 我改为 com.fasterxml.* 并解决了它。惊人的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 2020-12-10
  • 2023-03-04
  • 2018-07-14
  • 1970-01-01
相关资源
最近更新 更多