如何在REST Assured中访问底层的Jackson ObjectMapper?

时间:2014-07-31 22:38:43

标签: java jackson rest-assured

我需要在REST Assured中配置底层的Jackson ObjectMapper。我正在使用REST Assured编写REST API测试,我需要定义一些过滤器来注册ObjectMapper,用于将我的对象序列化为JSON:

    String newTestSuite = "{\"name\":\"Added through Rest API\",\"description\":\"Test Description\",\"steps\":[]}";

    FilterProvider filters = new SimpleFilterProvider().addFilter("createNewTestSuite", new NewTestSuiteFilter());
    ObjectMapper om = new ObjectMapper();
    om.setFilters(filters);

    try {
        TestSuite ts = om.readValue(newCaspianTest, TestSuite.class);

        RequestSpecification requestSpec = new RequestSpecBuilder()
            .setBaseUri("https://somesite.com")
            .setBody(ts)
            .setUrlEncodingEnabled(false)
            .build();

        ResponseSpecification responseSpec = new ResponseSpecBuilder()
            .expectStatusCode(200)
            .expectStatusLine(Matchers.containsString("200 OK"))
            .build();

        RestAssured.given()
            .auth().basic("testUser","testPassword")
            .spec(requestSpec)
            .log().all()
            .post("/restendpoint")
                .then()
                .log().all()
                .spec(responseSpec);

    } catch(JsonParseException jpe) {

    } catch(JsonMappingException jme) {

    } catch(IOException ioe) {

    }
}

}

4 个答案:

答案 0 :(得分:15)

你可以试试这个:

RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
new Jackson2ObjectMapperFactory() {
        @Override
        public ObjectMapper create(Class aClass, String s) {
            FilterProvider filter = new SimpleFilterProvider().addFilter(...);
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setFilters(filter);
            return objectMapper;
        }
    }
));

答案 1 :(得分:1)

这将为您提供一个对象映射器,当后端开发人员决定添加新字段时,该映射器不会爆炸。

RestAssured.config = RestAssuredConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
    new Jackson2ObjectMapperFactory() {
      @Override
      public ObjectMapper create(Type cls, String charset) {
        ObjectMapper om = new ObjectMapper().findAndRegisterModules();
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return om;
      }          

    }
));

答案 2 :(得分:0)

@sanj的回答非常有帮助。我必须稍作修改,因为我正在使用RestAssuredMockMvc。

这是我将RestAssuredMockMvc配置为使用 snake_case 而不是 CamelCase 的方式:

RestAssuredMockMvc.config = RestAssuredMockMvcConfig.config().objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
            (type, s) -> {
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
                return objectMapper;
            }
    ));

答案 3 :(得分:0)

鉴于使用了“确保放心”的上下文,通常您经常需要修改或提供用于 some 请求的ObjectMapper,但不是全部。

您可以使用以下方法来实现:

一个经典的用例,如@jason-henriksen所述

  • 允许忽略未知属性
  • 在反序列化过程中忽略ReturnedType类中定义的未标记为@JsonIgnore的属性,而不会引发异常(在没有控件/无法修改该ReturnedType类的情况下很有用 1

这可以通过以下方式完成:

RequestSpecification request = ...;
io.restassured.mapper.ObjectMapper objectMapper = new Jackson2Mapper(((type, charset) -> {
    com.fasterxml.jackson.databind.ObjectMapper om = new ObjectMapper().findAndRegisterModules();
    om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return om;
}));
ReturnType response = request.get().as(ReturnType.class, objectMapper);

1 您还可以通过使用Jackson MixIns配置ObjectMapper来实现此目的。