Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

jersey - 是否可以在JSON-B(Yasson)中全局设置默认日期格式,而不是在每个属性上添加注释?(Is it possible to set the default date format in JSON-B (Yasson) globally, instead of adding an annotation on every property?)

I have using Jersey so far and I am doing my first implementation with JSON-B.

(到目前为止,我已经使用Jersey,并且正在使用JSON-B进行第一个实现。)

I am using Payara, so I working with Jersey and Yasson.

(我正在使用Payara,所以我与Jersey和Yasson合作。)

I had an issue, because the serialized dates would always contain the "[UTC]" suffix.

(我遇到了一个问题,因为序列化的日期将始终包含“ [UTC]”后缀。)

I have managed to use an annotation on my date property, in my DTO.

(我设法在DTO的date属性上使用了注释。)

But I would like to configure that globally (in the JAX-RS application config?), instead of repeating myself on every date property.

(但是我想全局配置(在JAX-RS应用程序配置中?),而不是在每个date属性上重复我自己。)

Is that possible?

(那可能吗?)

I haven't found anything so far...

(到目前为止我还没有发现任何东西...)

Side question: I assume that it is possible to get rid of this "[UTC]" suffix, since it breaks all clients trying to parse the date.

(附带的问题:我认为有可能摆脱此“ [UTC]”后缀,因为它会使尝试解析日期的所有客户端中断。)

Any idea?

(任何的想法?)

  ask by Olivier Liechti translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to this Github issue , I was able to solve my problem.

(由于这个Github问题 ,我得以解决我的问题。)

Here is what I ended up writing in my code:

(这是我最终在代码中编写的内容:)

JSONConfigurator.java:

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.config.PropertyNamingStrategy;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
public class JSONConfigurator implements ContextResolver<Jsonb> {

  @Override
  public Jsonb getContext(Class<?> type) {
    JsonbConfig config = getJsonbConfig();
    return JsonbBuilder
      .newBuilder()
      .withConfig(config)
      .build();
  }

  private JsonbConfig getJsonbConfig() {
    return new JsonbConfig()
      .withDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", null);
  }
}

And:

(和:)

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/api")    
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<Class<?>>();
        addRestResourceClasses(resources);
        resources.add(JSONConfigurator.class);
        return resources;
    }


    private void addRestResourceClasses(Set<Class<?>> resources) {
      ...
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...