简体   繁体   中英

Create custom java json spring boot parser for map<string, integer>

Error JSON parse error: Cannot coerce empty String ("") to element of `java.util.LinkedHashMap<java.lang.String,java.lang.Integer>`

when the http request is like field: {"one": 1} then it works

however, the field is nullable and sometimes there comes an http request where the field is like field: ""

is it possible to create an converter which checks if the string is null or blank and then returns null without trying to parse it to the map and get an error?

You can use Optional I guess.

And yes, you can convert it.

Here is an example of trying to set a value that might be Null without the use of Optional.

Am trying to set spent_hours for example after getting the value from node which is Map<String, Object>

setSpent_hours(node.get("total_spent_hours")== null ? "N/A" :node.get("total_spent_hours").toString());

This is basically setting the value to a string of N/A instant of keeping the value null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM