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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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中处理异常的最简单方法的主要内容,如果未能解决你的问题,请参考以下文章

从 Java 中的 URL 读取 JSON 的最简单方法

保护 WebServer 和 ApplicationServer 之间的内部 WCF 4.0 REST 服务的最简单方法是啥?

Spring Boot Endpoints 中的异常处理

WCF REST 完整服务中的异常处理

处理自定义转换器抛出的 Spring Boot REST 异常

Java REST Api(在 tomcat 上运行)- 在客户端和服务器之间获得实时同步的最简单方法