Search code examples
javauuidmapper

Cannot convert a valid UUID string to UUID using mapper.readValue


I have a valid UUID in string format

7a041f81-1214-41e5-bb58-9a46b2ca08d4

but when I user a ObjectMapper to convert it to a UUID I keep getting this error.

    UUID uuid = mapper.readValue("7a041f81-1214-41e5-bb58-9a46b2ca08d4",UUID.class);

error:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('a' (code 97)): Expected space separating root-level values at [Source: (String)"7a041f81-1214-41e5-bb58-9a46b2ca08d4"; line: 1, column: 3] at com.xxxx.yyyyy.zzzzz.Test.callTest(BmcEventListenerTest.java:22

how can I convert the string to UUID and why do I keep getting this error?


Solution

  • You don't need Object mapper, you can do the following

    UUID obj = UUID.fromString("7a041f81-1214-41e5-bb58-9a46b2ca08d4"); 
    

    Demo: https://onecompiler.com/java/3v2sr8pk8