Hett
Hett

Reputation: 3831

spring framwork: UUID in @RequestBody

Next example works fine:

@PostMapping("/test/{id}")
public ResponseEntity test(@PathVariable UUID id)
{
    System.out.println(id);
    return ResponseEntity.ok().build();
}

but when I try to use UUID in RequestBody

@PostMapping("/test")
public ResponseEntity test(@RequestBody TestDTO testDTO)
{
    System.out.println(testDTO.id);
    return ResponseEntity.ok().build();
}

then I get null in console

id field has UUID type

public class TestDTO {
    UUID id;
}

if replaced UUID by String, then code works fine.

my request look like this

curl -XPOST -H "Content-Type: application/json" -d '{"id": "00000000-0000-0000-0000-000000000000"}' localhost:8080/test/test

how to fix it using UUID ?

Upvotes: 1

Views: 5339

Answers (2)

Vinit Solanki
Vinit Solanki

Reputation: 2033

If you add getter/setters of the id properties then you will get the value of it.

here I found one good demonstration about the JSON Marshalling

Upvotes: 1

Hett
Hett

Reputation: 3831

Annotated with @JsonDeserialize field works fine:

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.util.UUID;

public class TestDTO {
    @JsonDeserialize
    UUID id;
}

UPD:

this solutions works too without annotations:

public class TestDTO {

    private UUID id;

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }
}

Upvotes: 2

Related Questions