简体   繁体   English

如何配置 GraphQL SPQR 以使用 Gson 而不是 Jackson

[英]How to configure GraphQL SPQR to use Gson instead of Jackson

I noticed that GraphQL-SPQR has at least a couple properties that you can configure (eg graphql.spqr.gui.enabled=true ).我注意到 GraphQL-SPQR 至少有几个可以配置的属性(例如graphql.spqr.gui.enabled=true )。 Is there a configuration for specifying to use Gson instead of Jackson for object serialization/deserialization?是否有指定使用 Gson 而不是 Jackson 进行对象序列化/反序列化的配置?

If there is not configuration, is there some other way I can use Gson instead of Jackson for GraphQL-SPQR?如果没有配置,是否有其他方法可以使用 Gson 而不是 Jackson 进行 GraphQL-SPQR?

NOTE: I am already using spring.http.converters.preferred-json-mapper=gson in my application.properties , to override spring in general to use Gson instead of Jackson, but it doesn't seem to override GraphQL-SPQR's value mapper (defaults to Jackson).注意:我已经在我的application.properties使用spring.http.converters.preferred-json-mapper=gson ,一般覆盖 spring 以使用 Gson 而不是 Jackson,但它似乎没有覆盖 GraphQL-SPQR 的值映射器(默认为杰克逊)。

Any help is greatly appreciated :)任何帮助是极大的赞赏 :)

All you need to do is register a ValueMapperFactory bean, eg您需要做的就是注册一个ValueMapperFactory bean,例如

@Bean
public ValueMapperFactory gson() {
    return new GsonValueMapperFactory();
}

To customize the factory, use the builder instead of the constructor:要自定义工厂,请使用构建器而不是构造器:

@Bean
public ValueMapperFactory gson(Gson gson) {
    return GsonValueMapperFactory.builder()
            .withPrototype(gson)
            .build();
}

You can find most customizable beans listed here .您可以在此处找到大多数可定制的 bean。

I also think respecting spring.http.converters.preferred-json-mapper is a great idea.我也认为尊重spring.http.converters.preferred-json-mapper是一个好主意。 I'll add that in a future release.我会在以后的版本中添加它。 Follow this issue for progress.请关注此问题以获取进展。

Be warned though that Gson mapping is much simpler than Jackson's.但请注意,Gson 映射比 Jackson 的映射简单得多。 It has a lot less features, only takes fields into accounts (not constructor parameters or setters) and will likely not behave exactly the same as Jackson-based mapping.它的功能要少得多,只考虑字段(不是构造函数参数或设置器),并且可能与基于 Jackson 的映射的行为不完全相同。 But I guess you're aware of that since you explicitly wish to use it.但我想您已经意识到这一点,因为您明确希望使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM