4

I have example controller method

public void generateFile(@RequestBody final FileRequest request) {
...
}

Sometimes not all fields of this class FileRequest are filled, is there any way to set the default value when the value in the request is empty or null?

I mean something like @Default

merc-angel
  • 394
  • 1
  • 5
  • 13

1 Answers1

3

In FileRequest class, setting field with a value. If field not filled, it will use default value in class. Use lombok, class is too simple. like as below:

// class User
import lombok.Data;

@Data
public class User {
    private String name;
    private String address="beijing";
    private int age=10;
}

// in Class restConctroller

   @RequestMapping(value = "/res1/data")
    public Object postData(@RequestBody User user){
        return user;
    }

after post http://localhost:8080/res1/data with name='aaa', you will get result as

{
  "name": "aaa",
  "address": "beijing",
  "age": 10
}
jacky-neo
  • 763
  • 4
  • 7