我在连接 Jackson 的 Afterburner 模块时遇到了一些问题。下面的测试用例可以最好地总结这个问题。为冗长的例子道歉;对于这个练习,我尽量保持简短。当您注释掉注册模块的行并在您将其保留时失败时,测试有效。
我将 Jackson 2.1.1 用于注释、核心和加力。
我会很感激你可能有的任何想法。
谢谢!
帽子
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class AfterburnerTest {
@Test
public void mapOccupancyNoMaxAdults() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new AfterburnerModule());
final JsonNode node = objectMapper.readTree(
"{" +
"\"max\":3," +
"\"adults\": {" +
"\"min\":1" +
"}," +
"\"children\":{" +
"\"min\":1," +
"\"max\":2" +
"}" +
"}");
final Occupancy occupancy = objectMapper.reader(Occupancy.class).readValue(node);
assertNull(occupancy.getAdults().getMax());
assertNotNull(occupancy.getChildren().getMax());
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Occupancy {
private Integer max;
private Guests adults;
private Guests children;
public Occupancy() {
}
public Occupancy(Integer max, Guests adults, Guests children) {
this.max = max;
this.adults = adults;
this.children = children;
}
public Integer getMax() {
return max;
}
public Guests getAdults() {
return adults;
}
public Guests getChildren() {
return children;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Guests {
private Integer min;
private Integer max;
public Guests() {
}
public Guests(Integer min, Integer max) {
this.min = min;
this.max = max;
}
public Integer getMin() {
return min;
}
public Integer getMax() {
return max;
}
}
}