What is the best way to return different types of ResponseEntity in Spring MVC or Spring-Boot

后端 未结 12 1503
南旧
南旧 2020-12-07 09:15

I have written simple rest application using Spring MVC 4 (or Spring-Boot). Within the controller I have return ResponseEntity. But in some cases I want to give

12条回答
  •  臣服心动
    2020-12-07 09:32

    Its possible to return ResponseEntity without using generics, such as follows,

    public ResponseEntity method() {
        boolean isValid = // some logic
        if (isValid){
            return new ResponseEntity(new Success(), HttpStatus.OK);
        }
        else{
            return new ResponseEntity(new Error(), HttpStatus.BAD_REQUEST);
        }
    }
    

提交回复
热议问题