1

I have a Spring Boot application with a controller that has an endpoint that accepts an object as a parameter, like this:

@RestController
public class MyController {
    @PostMapping("/Student")
    public String createStudent(@RequestBody Student student) {
        //validate and persist Student object
        return "ok";
    }
}

My problem is that the Student object has an overloaded setter method, and whenever I POST to this endpoint I get a Jackson error saying that there are conflicting setter methods and it failed to parse.

I have searched online and it appears the solution is to add @JsonIgnore to one of the setter methods, however the Student object is from a 3rd party library and I cannot edit it at all.

So how can I solve this? Is there an easy 'Spring' way of making this work? I have played around with modifying Spring's Jackson HTTP message converter but had no success

DraegerMTN
  • 1,001
  • 2
  • 12
  • 21

1 Answers1

2

Use MixIn feature.

interface StudentMixIn {
    @JsonIgnore
    void setName(String name);
}

And register it in ObjectMapper overriding Jackson message converter.

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146