下面列出了怎么用com.fasterxml.jackson.databind.exc.InvalidTypeIdException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public JsonMappingException invalidTypeIdException(JavaType baseType, String typeId,
String extraDesc) {
String msg = String.format("Could not resolve type id '%s' as a subtype of %s",
typeId, baseType);
return InvalidTypeIdException.from(null, _colonConcat(msg, extraDesc), baseType, typeId);
}
@Override
public JsonMappingException invalidTypeIdException(JavaType baseType, String typeId,
String extraDesc) {
String msg = String.format("Could not resolve type id '%s' as a subtype of %s",
typeId, baseType);
return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, typeId);
}
/**
* @since 2.9
*/
public JsonMappingException missingTypeIdException(JavaType baseType,
String extraDesc) {
String msg = String.format("Missing type id when trying to resolve subtype of %s",
baseType);
return InvalidTypeIdException.from(_parser, _colonConcat(msg, extraDesc), baseType, null);
}
@Test(expected = InvalidTypeIdException.class)
public void whenTypeIsEmptyString_throwsException() throws IOException {
final String json = "{" +
"\"name\":\"some-name\"," +
"\"type\":\"\"," +
"\"value\":\"some-value\"," +
"\"overwrite\":true" +
"}";
JsonTestHelper.deserializeChecked(json, BaseCredentialSetRequest.class);
}
@Test(expected = InvalidTypeIdException.class)
public void whenTypeIsUnknown_throwsException() throws IOException {
final String json = "{" +
"\"name\":\"some-name\"," +
"\"type\":\"moose\"," +
"\"value\":\"some-value\"," +
"\"overwrite\":true" +
"}";
JsonTestHelper.deserializeChecked(json, BaseCredentialSetRequest.class);
}
public void testInvalidTypeIdFail() throws Exception
{
try {
MAPPER.readValue("{\"value\":{\"type\":\"foo\",\"a\":4}}",
BaseWrapper.class);
fail("Should not pass");
} catch (InvalidTypeIdException e) {
verifyException(e, "Could not resolve type id 'foo'");
assertEquals(Base.class, e.getBaseType().getRawClass());
assertEquals("foo", e.getTypeId());
}
}
@Test
public void testToResponseSetsPredefinedStatusWhenExceptionIsDerivedFromExpectedException() {
InvalidTypeIdException derivedFromJsonMappingException = new InvalidTypeIdException(null, "Invalid type", null, "any type id");
Response response = new GeneralExceptionMapper().toResponse(derivedFromJsonMappingException);
assertThat(response.getStatus(), is(BAD_REQUEST.getStatusCode()));
}