本文整理汇总了Java中com.fasterxml.jackson.databind.exc.InvalidFormatException类的典型用法代码示例。如果您正苦于以下问题:Java InvalidFormatException类的具体用法?Java InvalidFormatException怎么用?Java InvalidFormatException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InvalidFormatException类属于com.fasterxml.jackson.databind.exc包,在下文中一共展示了InvalidFormatException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deserialize
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Override
public ListResource<T> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException {
JsonNode node = parser.getCodec().readTree(parser);
// Find the first unrecognised field that is an array
Optional<Map.Entry<String, JsonNode>> field = CommonUtil.stream(node.fields())
.filter(f -> !RECOGNISED_KEYS.contains(f.getKey()))
.filter(f -> f.getValue().isArray())
// There might be multiple fields that are arrays, so we pick the first one and ignore the others
.sorted().findFirst();
if (!field.isPresent()) {
throw InvalidFormatException.from(parser, ListResource.class, "Expected a field containing a list");
}
String key = field.get().getKey();
JsonNode value = field.get().getValue();
List<T> items = value.traverse().readValueAs(new TypeReference<List<T>>(){});
Link selfLink = node.get("@id").traverse().readValueAs(Link.class);
return ListResource.create(selfLink, key, items);
}
示例2: handleInputNotReadableException
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@ExceptionHandler({HttpMessageNotReadableException.class, InvalidJsonException.class, InvalidFormatException.class})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ResponseError handleInputNotReadableException(Exception exception) {
final Throwable cause = exception.getCause() == null ? exception : exception.getCause();
if (cause instanceof UnrecognizedPropertyException) {
return constructError("error.invalid_json_key", ((UnrecognizedPropertyException) cause).getPropertyName());
} else if (cause instanceof InvalidTypeIdException
|| (cause instanceof JsonMappingException && cause.getMessage()
.contains("missing property 'type'"))) {
return constructError("error.invalid_type_with_set_prompt");
} else if (cause instanceof InvalidFormatException) {
for (InvalidFormatException.Reference reference : ((InvalidFormatException) cause)
.getPath()) {
if ("operations".equals(reference.getFieldName())) {
return constructError("error.permission.invalid_operation");
}
}
}
return badRequestResponse();
}
示例3: createParsingError
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
private ErrorListDto createParsingError(InvalidFormatException invalidFormatException) {
//Get the Name of the Processor
String nameOfProcessor = invalidFormatException.getProcessor().getClass().getSimpleName();
//Decide between different input types like excel or json files
switch (nameOfProcessor) {
//Excel Parsing Error
case "TreeTraversingParser":
return this.createExcelParsingError(invalidFormatException);
//Json Parsing Error
case "UTF8StreamJsonParser":
return this.createJsonParsingError(invalidFormatException);
default:
return new ErrorListDto();
}
}
示例4: createJsonParsingError
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
private ErrorListDto createJsonParsingError(InvalidFormatException invalidFormatException) {
UTF8StreamJsonParser processor =
(UTF8StreamJsonParser) invalidFormatException.getProcessor();
//Create Json Parsing Error. Just the first will be returned
try {
String domainObject = ((Class<?>) invalidFormatException.getPath().get(0).getFrom())
.getSimpleName();
String property = processor.getCurrentName();
if (property == null) {
property = invalidFormatException.getPath().get(0).getFieldName();
}
String invalidValue = (String)invalidFormatException.getValue();
String messageKey = "global.error.import.json-parsing-error";
return new ErrorListDto(new ErrorDto(domainObject, messageKey,invalidValue, property));
} catch (IOException e) {
return new ErrorListDto(
new ErrorDto(null, "global.error.import.no-json-mapping", null, null));
}
}
示例5: from
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
public static Error from(JsonMappingException e) {
String path = e.getPath().stream().map(x -> x.getFieldName()).collect(Collectors.joining(" -> "));
if (e instanceof InvalidFormatException) {
InvalidFormatException ex = (InvalidFormatException) e;
return new Error(
String.format("Invalid value for %s. Expected value of type %s but got: '%s'", path, ex.getTargetType().getSimpleName(), ex.getValue()),
e
);
} else if (e instanceof IgnoredPropertyException) {
return new Error(String.format("Missing value for %s", path), e);
} else if (e instanceof UnrecognizedPropertyException) {
return new Error(String.format("Unknown property: %s", path), e);
} else {
return new Error(String.format("Cannot parse JSON data for property %s", path), e);
}
}
示例6: arrayItemsAreNotRecursivelyMerged
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Test
public void arrayItemsAreNotRecursivelyMerged() throws Exception {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/yaml/simplePropertiesInArrayItem.yaml", "com.example", config("sourceType", "yaml"));
Class<?> genType = resultsClassLoader.loadClass("com.example.SimplePropertiesInArrayItem");
// Different array items use different types for the same property;
// we don't support union types, so we have to pick one
assertEquals(Integer.class, genType.getMethod("getScalar").getReturnType());
thrown.expect(InvalidFormatException.class);
thrown.expectMessage(startsWith("Cannot deserialize value of type `java.lang.Integer` from String \"what\": not a valid Integer value"));
OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/yaml/simplePropertiesInArrayItem.yaml"), Array.newInstance(genType, 0).getClass());
}
示例7: arrayItemsAreNotRecursivelyMerged
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Test
public void arrayItemsAreNotRecursivelyMerged() throws Exception {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/json/simplePropertiesInArrayItem.json", "com.example", config("sourceType", "json"));
Class<?> genType = resultsClassLoader.loadClass("com.example.SimplePropertiesInArrayItem");
// Different array items use different types for the same property;
// we don't support union types, so we have to pick one
assertEquals(Integer.class, genType.getMethod("getScalar").getReturnType());
thrown.expect(InvalidFormatException.class);
thrown.expectMessage(startsWith("Cannot deserialize value of type `java.lang.Integer` from String \"what\": not a valid Integer value"));
OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/json/simplePropertiesInArrayItem.json"), Array.newInstance(genType, 0).getClass());
}
示例8: shouldThrowExceptionWhenLoadingConfigFileWithInvalidUserAccountAttributes
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Test
public void shouldThrowExceptionWhenLoadingConfigFileWithInvalidUserAccountAttributes() throws IOException {
String badAttribute = "[ \"FIRST_NAME\", \"FIRST_NAME_VERIFIED\", \"INVALID_ATTRIBUTE\" ]";
InputStream badAttributeStream = new ByteArrayInputStream(badAttribute.getBytes());
ObjectMapper mapper = new ObjectMapper();
exception.expect(InvalidFormatException.class);
StringContains matcher = new StringContains("\"INVALID_ATTRIBUTE\": value not one of declared Enum instance names: [MIDDLE_NAME_VERIFIED, MIDDLE_NAME, DATE_OF_BIRTH, CURRENT_ADDRESS_VERIFIED, FIRST_NAME, SURNAME, SURNAME_VERIFIED, FIRST_NAME_VERIFIED, CURRENT_ADDRESS, DATE_OF_BIRTH_VERIFIED, ADDRESS_HISTORY, CYCLE_3]");
exception.expectMessage(matcher);
mapper.readValue(badAttributeStream, mapper.getTypeFactory().constructCollectionType(List.class, UserAccountCreationAttribute.class));
}
示例9: toResponse
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Override
public Response toResponse(InvalidFormatException ex) {
String value = ObjectUtils.ifNotNull(ex.getValue(), Object::toString, "NULL");
return ResultStash.builder()
.setStatus(Response.Status.PRECONDITION_FAILED)
.addFieldError("Invalid JSON field detected.", "invalid.json.field", MapperUtils.printPropertyPath(ex), value)
.buildResponse();
}
示例10: resolveError
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Override
public JsonError resolveError(Throwable t, Method method, List<JsonNode> arguments) {
JsonError error = null;
if(t instanceof RskJsonRpcRequestException) {
error = new JsonError(((RskJsonRpcRequestException) t).getCode(), t.getMessage(), null);
} else if (t instanceof InvalidFormatException) {
error = new JsonError(-32603, "Internal server error, probably due to invalid parameter type", null);
} else {
logger.error("JsonRPC error when for method " + method + " with arguments " + arguments, t);
error = new JsonError(-32603, "Internal server error", null);
}
return error;
}
示例11: getErrorCodeDto
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
private ErrorCodeDto getErrorCodeDto(JsonProcessingException e) {
if(e instanceof InvalidFormatException) {
return new ErrorCodeDto(ErrorCodeDto.VMIDC_VALIDATION_EXCEPTION_ERROR_CODE, Arrays.asList(
"Value " + ((InvalidFormatException) e).getValue() + " is invalid"
));
}
return new ErrorCodeDto(ErrorCodeDto.VMIDC_VALIDATION_EXCEPTION_ERROR_CODE, Arrays.asList(
String.format("Parse exception. Invalid request: %s", e)
));
}
示例12: getInvalid
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
@Test
public void getInvalid() throws Exception {
try {
client.basics().getInvalid();
Assert.assertTrue(false);
} catch (Exception exception) {
// expected
Assert.assertEquals(InvalidFormatException.class, exception.getCause().getClass());
}
}
示例13: buildPropertyPath
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
/**
* Build and return a property path of given exception.
*/
private StringBuilder buildPropertyPath(final List<InvalidFormatException.Reference> path) {
final StringBuilder propertyPath = new StringBuilder();
InvalidFormatException.Reference parent = null;
for (final InvalidFormatException.Reference reference : path) {
buildPropertyPath(propertyPath, reference, parent);
parent = reference;
}
return propertyPath;
}
示例14: buildNestedPropertyPath
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
/**
* Build nested property path.
*/
private void buildNestedPropertyPath(final StringBuilder propertyPath, final InvalidFormatException.Reference reference) {
if (reference.getIndex() > -1) {
propertyPath.append('[');
propertyPath.append(reference.getIndex());
propertyPath.append(']');
} else {
propertyPath.append('.');
}
}
示例15: parseRule
import com.fasterxml.jackson.databind.exc.InvalidFormatException; //导入依赖的package包/类
/**
* Parse the rule name from the Jackson mapping exception message in the given violation.
*/
private static String parseRule(final InvalidFormatException mappingException) {
final String rule = StringUtils.capitalize(mappingException.getTargetType().getSimpleName());
// Manage the primitive type "int" due to Jackson 2.x new features
return "Int".equals(rule) ? "Integer" : rule;
}