1

Is there any way to find outthat input JSON contains multiple times concrete parameter in Spring REST? For instance if I have controller:

@PostMapping("/")
public void handle(@RequestBody Id id) {
    ...
}

Where object Id is classic POJO

public class Id {

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

And in request json is sent twice parameter id

{
    "id" : "value",
    "id" : "different value"
}

is possible to find out that id was send twice?

Solution: I add to application.proeprties:

spring.jackson.parser.strict-duplicate-detection=true
Roman
  • 23
  • 5
  • Did you test that already? What happened? – Thomas Mar 11 '21 at 09:12
  • One thing you could do is enable Jackson's strict validation, i.e. to complain on duplicate properties/keys etc. This might help: https://stackoverflow.com/questions/56052262/how-do-i-enable-strict-validation-of-json-jackson-requestbody-in-spring-boot – Thomas Mar 11 '21 at 09:15
  • It rewrite first id parameter with second one. I check link you sent. – Roman Mar 11 '21 at 09:42
  • Note that the link I've sent is just about how to enable those properties/features for Jackson but it does not cover the one you actually need. Refer to the Jackson documentation for what features are available, e.g. [FAIL_ON_READING_DUP_TREE_KEY](https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/DeserializationFeature.html#FAIL_ON_READING_DUP_TREE_KEY) – Thomas Mar 11 '21 at 09:49
  • I tried this one you mentioned but not working in Spring Boot rest, only in custom deserialization. But with your help I found something similiar which solved this issue. I used in properties spring.jackson.parser.strict-duplicate-detection=true which throws json parser exception. Thank for help – Roman Mar 11 '21 at 11:31

1 Answers1

1

To handle duplicities set in application.proeprties

spring.jackson.parser.strict-duplicate-detection=true

with this settings application will throw on duplicity parameter

com.fasterxml.jackson.core.JsonParseException: Duplicate field 'id'
Roman
  • 23
  • 5