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

后端 未结 12 1505
南旧
南旧 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:46

    You can return generic wildcard to return Success and Error on a same request mapping method

    public ResponseEntity method() {
        boolean b = // some logic
        if (b)
            return new ResponseEntity(HttpStatus.OK);
        else
            return new ResponseEntity(HttpStatus.CONFLICT); //appropriate error code
    }
    

    @Mark Norman answer is the correct approach

提交回复
热议问题