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

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

    You can use a map with your object or string like bellow :

    @RequestMapping(value = "/path", 
            method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
        @ResponseBody
        public ResponseEntity> getData(){
    
        Map response = new HashMap();
    
        boolean isValid = // some logic
        if (isValid){
            response.put("ok", "success saving data");
            return ResponseEntity.accepted().body(response);
        }
        else{
            response.put("error", "an error expected on processing file");
            return ResponseEntity.badRequest().body(response);
        }
    
    }
    

提交回复
热议问题