I have some REST mappings, for example:
@ApiOperation(value = "Process passport data")
@PostMapping(path = "/{sourceType}/passport", consumes = MediaType.APPLICATION_JSON_VALUE)
public CommonResponse passport(@PathVariable @NotBlank final String sourceType,
@RequestBody @Valid final PassportRequest passport) {
return service.processPassport(sourceType, passport);
}
and PassportRequest
like
@Data
@Accessors(chain = true)
public class PassportRequest {
private UUID appId;
...
Some clients send to server invalid uuid
values. They uppercase uuid
and remove hyphens. Like
123E4567E89B12D3A456426614174000
How can I preprocess incoming guid string, add hyphens and lowercase them? I have hundreds of REST mappings, and UUID parameters in request forms may have different names.
UPDATE
Working solution - use Json Deserializer. Add class
public class UuidPatchDeserializer extends StdDeserializer<UUID> {
public UuidPatchDeserializer() {
super(UUID.class);
}
@Override
public UUID deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
final String value = _parseString(p, ctxt);
if (isSysGuid(value)) {
return UUID.fromString(sysGuidToUuid(value));
}
return UUID.fromString(value);
}
private boolean isSysGuid(final String id) {
return id.trim().matches("[A-F0-9]{32}");
}
private String sysGuidToUuid(final String id) {
return String.format("%s-%s-%s-%s-%s", id.substring(0,8), id.substring(8,12), id.substring(12, 16),
id.substring(16, 20), id.substring(20, 32)).toLowerCase(Locale.ROOT);
}
}
Annotate UUID fields in request dto
@Data
@Accessors(chain = true)
public class PassportRequest {
@JsonDeserialize(using = UuidPatchDeserializer.class)
private UUID appId;
...