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
5
  • Did you test that already? What happened? Commented Mar 11, 2021 at 9:12
  • One thing you could do is enable Jackson's strict validation, i.e. to complain on duplicate properties/keys etc. This might help: stackoverflow.com/questions/56052262/… Commented Mar 11, 2021 at 9:15
  • It rewrite first id parameter with second one. I check link you sent. Commented Mar 11, 2021 at 9: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 Commented Mar 11, 2021 at 9: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 Commented Mar 11, 2021 at 11:31

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.