Langley February 2016

How do I handle an 'undefined' value in a json request in Jackson/SpringMVC?

When I do a POST request to a @RestController like this:

{ "name": undefined }

I get the following response:

{
    ...
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "Could not read document: Unrecognized token 'undefined': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@5f9559a4; line: 3, column: 27]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'undefined': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@5f9559a4; line: 3, column: 27]",
    ...
}

I'm looking for a general solution to tell jackson to always map/deserialize undefined as null. I haven't found a single example or documentation that refers to handling undefined values so any pointers would be apreciated.

Answers


DwB February 2016

The Real Answer

Dont send undefined.

Bad Answer

Define the value as a String in your target object then wrap it in methods. Perhaps, something like this:

private String blammy;

public boolean isBlammyTrue()
{
    return "true".equalsIgnoreCase(blammy);
}

public boolean isBlammyFalse()
{
    return !isBlammyTrue();
}

Post Status

Asked in February 2016
Viewed 1,675 times
Voted 6
Answered 1 times

Search




Leave an answer