Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestController : reject request with unknown fields

I have the following endpoint :

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
public class TestController {

    @RequestMapping(value = "/persons", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<Integer> create(@RequestBody Person person) {
        // create person and return id
    }
}

Today if I received a request with an unknown field like this :

{
    "name" : "Pete",
    "bijsdf" : 51
}

I create the person and ignore the unknown field.

How can I check that there's an unknown field and then return a bad request ?

like image 694
Maxence Cramet Avatar asked Apr 08 '15 16:04

Maxence Cramet


1 Answers

Spring (4.1.2-RELEASE) use it's Jackson2ObjectMapperBuilder that by default disable FAIL_ON_UNKNOWN_PROPERTIES on overload jackson default behaviour. See this link to configure spring. Thx all for your helps

like image 156
Maxence Cramet Avatar answered Sep 22 '22 08:09

Maxence Cramet