标签:code 封装 结果 Code static Result Integer public
Result 结果集封装
Controller
-
Result
package com.yang.controller;
public class Result {
private Object data;
private Integer code;
private String msg;
public Result() {
}
public Result(Object data, Integer code) {
this.data = data;
this.code = code;
}
public Result(Object data, Integer code, String msg) {
this.data = data;
this.code = code;
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
-
Code
package com.yang.controller;
public class Code {
public static final Integer SAVE_OK = 20011;
public static final Integer UPDATE_OK = 20011;
public static final Integer DELETE_OK = 20011;
public static final Integer GET_OK = 20011;
public static final Integer SAVE_ERR = 20010;
public static final Integer UPDATE_ERR = 20010;
public static final Integer DELETE_ERR = 20010;
public static final Integer GET_ERR = 20010;
public static final Integer SYSTEM_UNKNOWN_ERROR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERROR = 50002;
public static final Integer PROJECT_VALIDATE_ERROR = 60001;
public static final Integer PROJECT_BUSINESS_ERROR = 60002;
}
-
BookController
package com.yang.controller;
import com.yang.domain.Book;
import com.yang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public Result save(@RequestBody Book book) {
boolean flag = bookService.save(book);
return new Result(flag, flag ? Code.SAVE_OK:Code.SAVE_ERR);
}
@PutMapping
public Result update(@RequestBody Book book) {
boolean flag = bookService.update(book);
return new Result(flag, flag ? Code.UPDATE_OK:Code.UPDATE_ERR);
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
boolean flag = bookService.delete(id);
return new Result(flag, flag ? Code.DELETE_OK:Code.DELETE_ERR);
}
@GetMapping("/{id}")
public Result select(@PathVariable Integer id) {
Book book = bookService.select(id);
Integer code = book !=null ?Code.GET_OK:Code.GET_ERR;
String msg = book !=null ? "" :"数据查询失败,请重试!";
return new Result(book,code,msg);
}
@GetMapping
public Result selectAll() {
List<Book> bookList = bookService.selectAll();
Integer code = bookList !=null ?Code.GET_OK:Code.GET_ERR;
String msg = bookList !=null ? "" :"数据查询失败,请重试!";
return new Result(bookList,code,msg);
}
}
-
ProjectExceptionAdvice
package com.yang.controller;
import com.yang.exception.BusinessException;
import com.yang.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException be ){
//发送对应的消息给用户
return new Result(null,be.getCode(),be.getMessage());
}
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException se ){
//发送特定消息给用户
//发送特定消息给运维人员,提醒维护
//记录日志
return new Result(null,se.getCode(),se.getMessage());
}
@ExceptionHandler(Exception.class)
public Result doException(Exception ex ){
//发送特定消息给用户
//发送特定消息给编程人员,提醒维护
//记录日志
return new Result(null,Code.SYSTEM_UNKNOWN_ERROR,"系统繁忙,请联系管理员");
}
}
标签:code,封装,结果,Code,static,Result,Integer,public 来源: https://www.cnblogs.com/yzlworld/p/16500311.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。