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);
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值