本文介绍了如何通过Genson/Jersey配置日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey提供我的RESTful服务,并使用Genson进行JSON/POJO转换. Genson没有设置,我只是将其放到类路径中并且可以正常工作,除了它在日期解析中引发错误外,因为格式是意外的.

I'm using Jersey for my RESTful services and Genson to perform my JSON/POJO conversion. There's no setup for Genson, I just drop it into the classpath and it just works, except that it throws an error on date parsing because the format is unexpected.

现在,如果我要使用Gson作为servlet来执行此操作,则需要在要维护的Gson实例上设置日期格式.这迫使POJO的解析使用正确的格式.我看到Genson具有类似的界面,但是我不知道如何从Jersey servlet服务或Spring上下文中获取实例,因此我无法设置格式.

Now, if I were to do this as a servlet, using Gson, I set the date format on a Gson instance that I maintain. That forces the parse of the POJO to use the correct format. I see that Genson has a similar interface, but I don't know how to get the instance from the Jersey servlet service or maybe the Spring context so that I cat set the format.

所以,简短的问题是:从泽西岛开始,如何设置Genson的日期格式?

So, the short question is: how do I set a date format for Genson when started through Jersey?

推荐答案

要配置Genson实例,可以使用Genson.Builder类(在这一点上,它类似于Gson).然后,您必须将其注入Jersey.

To configure Genson instances you can use Genson.Builder class (it is similar to Gson on this point). Then you have to inject it with Jersey.

@Component
@Provider
public class GensonProvider implements ContextResolver<Genson> {
   private final Genson genson = new Genson.Builder().setDateFormat(yourDateFormat).create();

    @Override
   public Genson getContext(Class<?> type) {
     return genson;
   }
}

您可能还想看看Genson如何集成到Jersey中此处.

You might also want to have a look at how Genson is integrated into Jersey here.

这篇关于如何通过Genson/Jersey配置日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:50