I have created DTO , which is having fields, associated with different jsonview, so that only those vields are there in RequestBody and ResponseBody DTO. Below is the sample code:
public class Test {
@JsonView({
//Request Views
CustomerPutOpsRequestBody.class,
CustomerGetOpsRequestBody.class,
CustomerQueryPutOpsRequestBody.class,
//Response views
CustomerGetOpsResponseBody.class,
CustomerPutOpsResponseBody.class,
CustomerQueryPutOpsResponseBody.class,
CustomerDeleteOpsResponseBody.class,
})
@NotBlank(message = "faqId cannot be null or empty",
groups = {
//Request Views
CustomerPutOpsRequestBody.class,
CustomerGetOpsRequestBody.class,
CustomerQueryPutOpsRequestBody.class,
})
private String faqId;
.
.
}
My controller looks something like below:
@JsonView({CustomerPutOpsResponseBody.class})
@PostMapping("/v1/faq/update")
public ResponseEntity<Test> createCustomer(@RequestBody
@JsonView({CustomerPutOpsRequestBody.class})
@Validated(CustomerPutOpsRequestBody.class) Test test) {
//Do something
}
I am facing 2 problems :
@NotBlank validation triggers , even when faqId is not the part of view of RequestDTO. I came across Grouping in validators,that too not working.
interface list in
@JsonView({..})
, keeps on growing , so is there is any alternative approach to put all interface in some other file and refer that inside@JsonView({<some reference for all interfaces> })
so as to keep things clean