0

I have to validate a JSON payload to check if all the request attributes mentioned on the Rest API Specification are passed. the value of some of these can be null. The use of JSON annotations like @NotNull, @NotEmpty is not suitable as they validate the value as well.

I have tried @Jsoncreator on a constructor. This solution works perfect for simple payloads but I am not clear on how to get this worked in case of complex payloads involving nested objects.

Another problem to highlight is that the same objects are used for both POST and PUT operation payloads. I need the validation for only PUT operations.

Can you please suggest on feasible solutions?

for a JSON request of 2 attributes - name, age -

valid payloads -

{"name" :"StackOverflow", "age" :99}

{"name" :"StackOverflow", "age" :null}

invalid payload - {"name" :"StackOverflow"}

Nitin
  • 21
  • 6
  • You can start from [@Valid when creating objects with jackson without controller](https://stackoverflow.com/questions/55457754/valid-when-creating-objects-with-jackson-without-controller). But I understand you need to check more whether "property" exists in payload or not so it must be checked before payload is deserialized to `POJO`. To make a question much cleaner - could you prepare some examples and provide expected validation output? – Michał Ziober Nov 21 '19 at 14:29
  • @michal ziobar - I have added some samples. hope this explains the requirements better. – Nitin Nov 22 '19 at 10:42
  • Do you want to deserialise this `JSON` to `POJO` or `Map`? How do you want to define which fields, paths, [JSON Pointer](http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-03) are required? Do you want to use annotations and specify them manually or read all properties from a `POJO` class and check each? – Michał Ziober Nov 22 '19 at 14:09

1 Answers1

0

I think your problem can be solved used by SpringMVC.

  1. nested bean validation

    Now we have a Customer class which involves nested class Address.
    If an instance of Customer is validated, the referenced Address object will be validated as well, as the address field is annotated with @Valid.
public class Customer {
  @NotBlank
  private String name;
  @NotBlank
  private String email;
  //add a @Valid annotation on nested class
  @Valid
  private Address address;

  //setter and getter
}
public class Address {
  @NotBlank
  private String country;
  @NotBlank
  private String city;

  //getter and setter

}
  1. validate only PUT

    You can use @PostMapping,@GetMapping,@PutMapping,@DeleteMapping....mapping HTTP requests onto specific handler methods.
  @PutMapping(value = "/customer")
  public String editCustomerInfo(@Valid Customer customer){
    System.out.println(customer.toString());
    return "put succeed";
  }
  @PostMapping(value = "/customer")
  public String addCustomerInfo(Customer customer){
    System.out.println(customer.toString());
    return "post succeed";
  }
Kevin Lee
  • 86
  • 2
  • Thank you for the inputs. Using NotBlank will be validating the value passed along with the tag. My requirement is to validate that the email tag is passed always but it need not have a value like below - {"name" : "StavkOverflow", "email" :null} – Nitin Nov 22 '19 at 10:37