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

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

    You can also implement like this to return Success and Error on a same request mapping method,use Object class(Parent class of every class in java) :-

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

提交回复
热议问题