Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate input before Jackson in Spring Boot

I've built a REST endpoint using Spring Boot. JSON is posted to the endpoint. Jackson converts the JSON giving me an object.

The JSON look like this:

{
    "parameterDateUnadjusted": "2017-01-01",
    "parameterDateAdjusted": "2017-01-02"
}

Jackson converts the JSON to an object based on this class:

public class ParameterDate {

    @NotNull(message = "Parameter Date Unadjusted can not be blank or null")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date parameterDateUnadjusted;
    @NotNull(message = "Parameter Date Adjusted can not be blank or null")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date parameterDateAdjusted;
    private Date parameterDateAdded;
    private Date parameterDateChanged;
}

This all works fine. The issue I'm having is that I would like to validate the data before Jackson converts the data. For instance if I post

{
    "parameterDateUnadjusted": "2017-01-01",
    "parameterDateAdjusted": "2017-01-40"
}

Where parameterDateAdjusted is not a valid date (there is no month with 40 days in it). Jackson converts this to 2017-02-09. One way of getting around this is to have a class that is only strings let's call it ParameterDateInput. Validate each filed with Hibernate Validator in the parameterDateInput object and then copy the parameterDateInput object to parameterDate where each field has the correct type (dates are of type Date and not of type String). This to me doesn't look like a very elegant solution. Is there some other way I can solve this? How is data generally validated in Spring Boot when posted as JSON? I like to be able to send back a message to the user/client what is wrong with the data that is being posted.

like image 907
g3blv Avatar asked Nov 07 '22 21:11

g3blv


1 Answers

How about a custom JSON deserializer where you can write down the logic you want:

@RestController
public class JacksonCustomDesRestEndpoint {

    @RequestMapping(value = "/yourEndPoint", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object createRole(@RequestBody ParameterDate paramDate) {
        return paramDate;
    }
}

@JsonDeserialize(using = RoleDeserializer.class)
public class ParameterDate {
    // ......
}

public class RoleDeserializer extends JsonDeserializer<ParameterDate> {
    @Override
    public ParameterDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);
        String parameterDateUnadjusted = node.get("parameterDateUnadjusted").getTextValue();
        //Do what you want with the date and set it to object from type ParameterDate and return the object at the end. 
        //Don't forget to fill all the properties to this object because you do not want to lose data that came from the request.
        return something;
    }
}
like image 69
Lazar Lazarov Avatar answered Nov 15 '22 06:11

Lazar Lazarov