REST中处理异常的最简单方法

在rest中,其实如果要抛出异常或者给出出錯信息,返回的相关的HTTP 状态码,最简单的
方法就是使用ResponseEntity,马上看例子:


@Controller
public class EmpController 
{
    List<Employee> list = new ArrayList<Employee>();  
     
    //Steps followed in this code are:
    //1) Validate the input
    //2) Do processing
    //3) Return appropriate HTTP code and message.
 
    @RequestMapping(value = "/getEmp/{emp}", method = RequestMethod.GET)
    public ResponseEntity<?> getEmployee(@PathVariable("emp") int empid) {
        if(empid < 0) {
            return new ResponseEntity<String>("Employee Id is not valid.", HttpStatus.BAD_REQUEST);
        }
         
        for (Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
            Employee emp = (Employee) iterator.next();
            if(emp.getEmpId()==empid) {
                return new ResponseEntity<Employee>(emp,HttpStatus.OK);
            }
        }
        return new ResponseEntity<String>("Employee with id: " + empid + " not found.", HttpStatus.NOT_FOUND);
    }
 
    @RequestMapping(value = "/removeEmp/{emp}", method = RequestMethod.DELETE)
    public ResponseEntity<?> removeEmployee(@PathVariable("emp") int empid) {
        if(empid < 0) {
            return new ResponseEntity<String>("Employee Id is not valid.", HttpStatus.BAD_REQUEST);
        }
 
        for (Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
            Employee emp = (Employee) iterator.next();
            if(emp.getEmpId()==empid) {
                iterator.remove();
                return new ResponseEntity<String>("Employee Successfully removed.", HttpStatus.OK);
            }
        }
        return new ResponseEntity<String>("Employee Not found.", HttpStatus.NOT_FOUND);
    }
 
    @RequestMapping(value = "/addEmp", 
            method = RequestMethod.POST)
    public ResponseEntity<?> addEmployee(@RequestBody Employee emp) {
 
        if(emp.getEmpId() < 0) {
            return new ResponseEntity<String>("Employee Id is not valid.", HttpStatus.BAD_REQUEST);
        }
 
        if(StringUtils.isBlank(emp.getDeptName())) {
            return new ResponseEntity<String>("Department name is not valid.", HttpStatus.BAD_REQUEST);
        }
 
        for (Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
            Employee tempEmp = (Employee) iterator.next();
            if(tempEmp.getEmpId()==emp.getEmpId()) {
                return new ResponseEntity<String>("Employee already present. No need to add again", HttpStatus.OK);
            }
        }
        list.add(emp);
        return new ResponseEntity<String>("Employee Successfully Added", HttpStatus.OK);
    }
 
    @RequestMapping(value = "/updateEmp", 
            method = RequestMethod.PUT)
    public ResponseEntity<?> updateEmployee(@RequestBody Employee emp) {
        if(emp.getEmpId() < 0) {
            return new ResponseEntity<String>("Employee Id is not valid.", HttpStatus.BAD_REQUEST);
        }
 
        if(StringUtils.isBlank(emp.getDeptName())) {
            return new ResponseEntity<String>("Department name is not valid.", HttpStatus.BAD_REQUEST);
        }
         
        for (Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
            Employee tempEmp = (Employee) iterator.next();
            if(tempEmp.getEmpId()==emp.getEmpId()) {
                tempEmp.setDeptName(emp.getDeptName());
                return new ResponseEntity<String>("Employee Successfully updated", HttpStatus.OK);
            }
        }
        return new ResponseEntity<String>("Employee Not found.", HttpStatus.NOT_FOUND);
    }
 
    @ExceptionHandler
    public ResponseEntity<String> exceptionHandler(Exception e){
        e.printStackTrace();
        return new ResponseEntity<String>("An internal error occurred while processing your request.", HttpStatus.INTERNAL_SERVER_ERROR);
    }
}


你可能感兴趣的:(REST)