简体   繁体   English

配置 RestAssured 以在 Jackson 上使用 GSON?

[英]Configure RestAssured to use GSON over Jackson?

I have both GSON and Jackson in a project using RestAssured and I want to use GSON.我在一个使用 RestAssured 的项目中同时拥有 GSON 和 Jackson,我想使用 GSON。 The official documentation does not provide a clear example.官方文档没有提供明确的例子。 I tried several configs, but it does not seem to work.我尝试了几个配置,但似乎不起作用。 Here's my config, am I missing something?这是我的配置,我错过了什么吗?

RestAssured.config = RestAssuredConfig.config()
                .decoderConfig(new DecoderConfig("UTF-8"))
                .encoderConfig(new EncoderConfig("UTF-8", "UTF-8"))
                .objectMapperConfig(new ObjectMapperConfig(GSON));

In my project I solved it by wrapping original RestAssured.given method在我的项目中,我通过包装原始的RestAssured.given方法解决了这个问题

public static RequestSpecification given() {
    return RestAssured.given()
        .config(RestAssured.config()
            .objectMapperConfig(new ObjectMapperConfig(ObjectMapperType.GSON)));
}

这在 Kotlin 中对我有用:

RestAssured.config = RestAssuredConfig.config().objectMapperConfig(objectMapperConfig().defaultObjectMapperType(ObjectMapperType.GSON))

Well, as the Rest Assured documentation states , the order of technologies is:好吧,正如Rest Assured 文档所述,技术顺序是:

  1. JSON using Jackson 2 (Faster Jackson (databind)) JSON 使用 Jackson 2 (Faster Jackson (databind))
  2. JSON using Jackson (databind) JSON 使用 Jackson(数据绑定)
  3. JSON using Gson使用 Gson 的 JSON
  4. XML using JAXB使用 JAXB 的 XML

Furthermore the use of an explicit serializer or deserializer is also described.此外,还描述了显式串行解串器的使用。

Serialization:序列化:

Message message = new Message();
message.setMessage("My messagee");
given().
   body(message, ObjectMapperType.GSON).
when().
  post("/message");

Deserialization:反序列化:

Message message = get("/message").as(Message.class, ObjectMapperType.GSON);

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

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