1

I have a POJO which I'm trying to reuse between 2 different modules (and will be used by 2 different controller methods in separate projects). I'm trying to apply a specific view on it in the controller method, so that the string thing will go through bean validation if it's scoped to a specific JsonView:

public class TestPojo {

    @JsonView(Views.TestView.class)
    @NotNull(message = "can't be empty")
    @JsonProperty
    private String thing;
}

My controller method is as follows:

@RequestMapping(value = "/api/test", method = POST)
public String test(@RequestBody @JsonView(Views.TestView.class) @Valid TestPojo request) {
    // omitted 
}

This validates fine, but if I change the above @JsonView to for example:

@JsonView(Views.OtherView.class)

I was then hoping it would no longer validate the variable thing because it's part of a different view. However in reality it's resolved to null (as you'd expect) but still gets validated and rejected.

Is there any way to get this to work as I'm hoping, or an easier way to do this that I've totally missed?

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
Pete
  • 1,500
  • 2
  • 16
  • 33
  • 1
    You can use *[Validation Groups](http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-groups.html)* like here http://stackoverflow.com/questions/35704351/spring-rest-controller-how-to-selectively-switch-off-validation – Ali Dehghani Oct 28 '16 at 05:00
  • 1
    That solves my problem, thank you sir! – Pete Oct 28 '16 at 05:59

0 Answers0