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

1 Answer 1

5

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.