1RestfulCRUD
- 1)RestfulCRUD:CRUD满足Restful风格
- URI:资源名称/资源标识 HTTP请求方式区别对资源CRUD的操作
普通CRUD | RestfulCRUD:CRUD | |
---|---|---|
查询 | getEmp | emp–GET |
添加 | addEmp?xxx | emp–POST |
修改 | modifyEmp?id=XXX&XXX=XXX | emp{id}–PUT |
删除 | deleteEmp ?id=XXX | emp–DELETE |
- 2)实验的请求架构:
请求URI | 请求方式 | |
---|---|---|
查询所有 | emps | GET |
查询单个(来到修改页面) | emp/{id} | GET |
添加 | emp | POST |
修改 | emp | PUT |
删除 | emp/{id} | DELETE |
- 3)具体的实现【都是伪代码】
@Autowired
EmployeeDao employeeDao;
//查询所有员工的列表
@GetMapping("/emps")
public String list() {
return employeeDao.getList();
}
//查询员工的详情
@GetMapping("/emp/{id}")
public String getOne(@PathVariable Integer id) {
return employeeDao.getOne(id);
}
//新增员工
@PostMapping("/emp")
public void Add(Employee employee) {
employeeDao.save(employee);
}
//修改员工
@PutMapping("/emp")
public void Modify(Employee employee) {
employeeDao.Modify(employee);
}
//删除员工
@DeleteMapping("/emp/{id}")
public void Delete(@PathVariable Integer id) {
employeeDao.Delete(id);
}
- 4)返回一个默认的错误页面
-
1.页面访问
-
2.客户端访问
原理:可以参考ErrorMvcAutoConfiguration:错误处理的自动配置
首先我们看一下SpringBoot中有哪些状态码:
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
PROCESSING(102, "Processing"),
CHECKPOINT(103, "Checkpoint"),
OK(200, "OK"),
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
NO_CONTENT(204, "No Content"),
RESET_CONTENT(205, "Reset Content"),
PARTIAL_CONTENT(206, "Partial Content"),
MULTI_STATUS(207, "Multi-Status"),
ALREADY_REPORTED(208, "Already Reported"),
IM_USED(226, "IM Used"),
MULTIPLE_CHOICES(300, "Multiple Choices"),
MOVED_PERMANENTLY(301, "Moved Permanently"),
FOUND(302, "Found"),
/** @deprecated */
@Deprecated
MOVED_TEMPORARILY(302, "Moved Temporarily"),
SEE_OTHER(303, "See Other"),
NOT_MODIFIED(304, "Not Modified"),
/** @deprecated */
@Deprecated
USE_PROXY(305, "Use Proxy"),
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
PERMANENT_REDIRECT(308, "Permanent Redirect"),
BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
PAYMENT_REQUIRED(402, "Payment Required"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
NOT_ACCEPTABLE(406, "Not Acceptable"),
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
REQUEST_TIMEOUT(408, "Request Timeout"),
CONFLICT(409, "Conflict"),
GONE(410, "Gone"),
LENGTH_REQUIRED(411, "Length Required"),
PRECONDITION_FAILED(412, "Precondition Failed"),
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
/** @deprecated */
@Deprecated
REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"),
URI_TOO_LONG(414, "URI Too Long"),
/** @deprecated */
@Deprecated
REQUEST_URI_TOO_LONG(414, "Request-URI Too Long"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested range not satisfiable"),
EXPECTATION_FAILED(417, "Expectation Failed"),
I_AM_A_TEAPOT(418, "I'm a teapot"),
/** @deprecated */
@Deprecated
INSUFFICIENT_SPACE_ON_RESOURCE(419, "Insufficient Space On Resource"),
/** @deprecated */
@Deprecated
METHOD_FAILURE(420, "Method Failure"),
/** @deprecated */
@Deprecated
DESTINATION_LOCKED(421, "Destination Locked"),
UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),
LOCKED(423, "Locked"),
FAILED_DEPENDENCY(424, "Failed Dependency"),
UPGRADE_REQUIRED(426, "Upgrade Required"),
PRECONDITION_REQUIRED(428, "Precondition Required"),
TOO_MANY_REQUESTS(429, "Too Many Requests"),
REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"),
UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable For Legal Reasons"),
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
NOT_IMPLEMENTED(501, "Not Implemented"),
BAD_GATEWAY(502, "Bad Gateway"),
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
GATEWAY_TIMEOUT(504, "Gateway Timeout"),
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version not supported"),
VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates"),
INSUFFICIENT_STORAGE(507, "Insufficient Storage"),
LOOP_DETECTED(508, "Loop Detected"),
BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded"),
NOT_EXTENDED(510, "Not Extended"),
NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required");
为容器中添加了以下组件
- 1、DefaultErrorAttributes
- 2、BasicErrorController:处理默认的/error请求
@Controller
@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class BasicErrorController extends AbstractErrorController {}
//会根据传过来的是不是带有text/html来判断返回HTML还是json
@RequestMapping(
produces = {"text/html"}
)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = this.getStatus(request);
Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = this.resolveErrorView(request, response, status, model);
return modelAndView != null ? modelAndView : new ModelAndView("error", model);
}
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = this.getStatus(request);
return new ResponseEntity(body, status);
}
- 3、ErrorMvcAutoConfiguration.ErrorPageCustomizer:系统出现错误以后来到error请求进行处理:(类似于web.xml注册的页面规则)
@Value("${error.path:/error}")
private String path = "/error";//
- 4、DefaultErrorViewResolver
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
ModelAndView modelAndView = this.resolve(String.valueOf(status.value()), model);
if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {
modelAndView = this.resolve((String)SERIES_VIEWS.get(status.series()), model);
}
return modelAndView;
}
步骤:
1、一旦系统出现4XX和5XX的错误,ErrorPageCustomizer就会生效(定制错误的响应规则);就会来到/error请求。
2、BasicErrorController接受到这些请求。会通过是否带有{text/html}来判断返回类型。
3、而判断的依据就是modelAndView的值。这个值的来源就是DefaultErrorViewResolver.。
4、最后根据DefaultErrorAttributes定义了返回的错误页面中存在的值需要哪些。
包含的内容主要有:timestamp
javax.servlet.error.status_code
javax.servlet.error.message
javax.servlet.error.request_uri
javax.servlet.error.exception
public class ErrorProperties {
@Value("${error.path:/error}")
private String path = "/error";
private boolean includeException;
private ErrorProperties.IncludeStacktrace includeStacktrace;
private final ErrorProperties.Whitelabel whitelabel;
- 5)如何定制错误响应:
-
1)如何定制错误页面:在静态资源文件夹或者模板引擎文件夹下防止一个ERROR文件夹就可以获取到新的
-
2)如何定制JSON返回串
首先我们定义一个跑出错误的类
//获取错误方法
@RequestMapping("/empErr/{id}")
public String getOneErr(@PathVariable Integer id) throws UserPrincipalNotFoundException {
if(id==0) {
throw new UserPrincipalNotFoundException("errorBug");
}
return employeeDao.getOne(id);
}
再写一个获取错误的handle
@ControllerAdvice
public class MyExceptionHandler {
@ResponseBody
@ExceptionHandler(UserPrincipalNotFoundException.class)
public Map<String,Object> handleException(UserPrincipalNotFoundException e)
{
Map<String,Object> map=new HashMap<>();
map.put("code","user.notExist");
map.put("message", e.getName());
return map;
}
}
我们可以看到json和页面都返回了新的自定义的
但是这样就失去了以前的一些错误信息,我们可以通过扩展DefaultErrorAttributes的方法来实现保证原本信息不变的情况下新增我们需要的错误信息。
我们理解一下:
首先出现错误之后,会来到/error请求,会被BasicErrorController处理,响应出去可以获取的数据是由getErrorAttributers得到的(是AbstractErrorController(ErrorController)规定的方法)
- 1、完全来编写一个ErrorController的实现类【或者是编写AbstractErrorController的子类】,放在容器中:
- 2、页面上能用的数据,或者是json返回能用的数据都是通过errorAttributers.getErrorAttributers得到:
-
容器中DefaultErrorAttributers.getErrorAttributers();默认进行数据处理
- 我们来对之前的MyExceptionHandler 进行一下重写,
- 【去掉@ResponseBody】
- 重定向到/error的时候需要手动设置状态码,默认javax.servlet.error.status_code=200
- 【新增request.setAttribute(“javax.servlet.error.status_code”,500);】
@ControllerAdvice
public class MyExceptionHandler {
// @ResponseBody
// @ExceptionHandler(Exception.class)
// @ResponseStatus(HttpStatus.BAD_REQUEST)
// public Map<String,Object> handleException(Exception e, WebRequest request)
// {
// Map<String,Object> map=new HashMap<>();
// request.setAttribute("javax.servlet.error.status_code",500,0);
// map.put("code","user.notExist");
// map.put("message", e.getMessage());
// request.setAttribute("ext",map,0);
// return map;
// }
@ExceptionHandler
public Map<String, Object> handleException(Exception e, HttpServletRequest request){
//重定向到/error的时候需要手动设置状态码,默认javax.servlet.error.status_code=200
request.setAttribute("javax.servlet.error.status_code",500);
//可以给MyErrorAttributes传入数据;
Map<String, Object> map = new HashMap<>();
map.put("handleCode","user.notExist");
request.setAttribute("map",map);
//return "forward:/error";
return map;
}
}
然后我们写一个MyErrorAttributes 来继承MyErrorAttributes 类,并且去重写他的getErrorAttributes方法。
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String,Object> map=super.getErrorAttributes(webRequest, includeStackTrace);
map.put("edit1","自定义1");
Map<String,Object> ext= (Map<String, Object>) webRequest.getAttribute("map",0);
map.put("map",ext);
return map;
}
}
来看一下执行效果:网页端恢复了。
客户端的错误回复
完成~