如何在杰克逊序列化中自定义日期,@JsonSerialize不起作用 [英] how to customize Date in jackson serialization , @JsonSerialize not working

查看:447
本文介绍了如何在杰克逊序列化中自定义日期,@JsonSerialize不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改java rest webservice传递的json中的日期格式,这是因为json具有这样的日期: 2019-05-23T06:00:00Z [UTC] ,因此由于'['和']'

,客户端将 [UTC] 与数组混淆了 我使用玻璃鱼5,jax-rs,杰克逊2.9.4 databind, .我曾尝试在模型对象中使用 @JsonSerialize(using = CustomXSerializer.class),但没有起作用,也 @JsonFormat(shape = JsonFormat.Shape.STRING,pattern ="MM-dd -yyyy,timezone =" CET),但在模型对象的Date属性中仍然无效,它始终显示[UTC]

我的代码:

package api;
import model.people;
import java.util.Date;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/helloworld")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class HelloWorldRest {

  @GET
@Produces(MediaType.APPLICATION_JSON)
   public people sayHello() {

    people p=new people("pepe", "27",new Date());
    return p;

  }
}

模型对象:

public class people {
    private String nombre;
    private String edad;
       @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="MM-dd-yyyy",timezone="CET")
    public Date d;
    public people(String pNombre,String pEdad,Date pD)
    { nombre=pNombre;
        edad=pEdad;
        d=pD;
    }
.
.
//getters and setters

pom:

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
    </dependencies>

如果所有模型对象的日期"序列化在一个位置,是否有任何方法可以更改格式?我宁愿代替为每个模型对象创建自定义序列化程序,

解决方案

我发现了我的问题所在,我需要将jackson注册为jersey中的json提供程序,因此可以像这样在web.xml中注册

<init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>

并使用pom上的依赖项,如下所示:

         <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.x</version>
        </dependency>

i want to change the date format in a json that is delivered by a java rest webservice, that is because the json have the dates like this: 2019-05-23T06:00:00Z[UTC] , so the client confuse the [UTC] with an array, because of the '[' and ']'

im using glassfish 5 ,jax-rs , jackson 2.9.4 databind, . i have tried to use @JsonSerialize(using = CustomXSerializer.class) in the model object and didn work, also @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="MM-dd-yyyy",timezone="CET") in the Date property in the model object but again didnt work, its always showing [UTC]

my code:

package api;
import model.people;
import java.util.Date;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/helloworld")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class HelloWorldRest {

  @GET
@Produces(MediaType.APPLICATION_JSON)
   public people sayHello() {

    people p=new people("pepe", "27",new Date());
    return p;

  }
}

the model object:

public class people {
    private String nombre;
    private String edad;
       @JsonFormat(shape= JsonFormat.Shape.STRING, pattern="MM-dd-yyyy",timezone="CET")
    public Date d;
    public people(String pNombre,String pEdad,Date pD)
    { nombre=pNombre;
        edad=pEdad;
        d=pD;
    }
.
.
//getters and setters

pom:

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
    </dependencies>

is there any way to change the format if the Date seralization in one spot for all the model objects? i would prefer that instead of creating custom serializer for each model object , thanks in advance

解决方案

i found what my problem was, i needed to register jackson as the json provider in jersey , so it can be registered in the web.xml like this

<init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>

and using the dependency at the pom like this:

         <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.x</version>
        </dependency>

这篇关于如何在杰克逊序列化中自定义日期,@JsonSerialize不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆