11

I have both GSON and Jackson in a project using RestAssured and I want to use 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));
vicusbass
  • 1,714
  • 2
  • 19
  • 33

3 Answers3

7

In my project I solved it by wrapping original RestAssured.given method

public static RequestSpecification given() {
    return RestAssured.given()
        .config(RestAssured.config()
            .objectMapperConfig(new ObjectMapperConfig(ObjectMapperType.GSON)));
}
Maciej Mikosik
  • 355
  • 1
  • 5
  • 14
4

This worked for me in Kotlin:

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

Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
2

Well, as the Rest Assured documentation states, the order of technologies is:

  1. JSON using Jackson 2 (Faster Jackson (databind))
  2. JSON using Jackson (databind)
  3. JSON using Gson
  4. XML using JAXB

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);
mle
  • 2,466
  • 1
  • 19
  • 25
  • 1
    I know what the official documentation states about the order of libraries, that's why I am wondering how do I configure RestAssured to use GSON by default. Serialization/deserialization is clear, that's exactly what I am doing, but my question is about the global configuration of RestAssured. Doing explicit serialization with GSON for each call is not really feasible. – vicusbass Mar 30 '18 at 07:01