1

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;
    ...
Ivan Ivanov
  • 2,076
  • 16
  • 33
  • Very similar to what you're looking for https://stackoverflow.com/questions/50932518/how-to-modify-request-body-before-reaching-controller-in-spring-boot – void void May 26 '22 at 20:35
  • @voidvoid I saw that solution 1. It is outdated, all methods are deprecated 2. It relies on parameter name. I want to bind to UUID class – Ivan Ivanov May 27 '22 at 06:09

0 Answers0