SpringBoot2--错误处理

前言

本文讨论Springboot2是如何处理页面访问错误的问题

一、默认错误处理机制

1.错误处理原理

在springboot中ErrorMvcAutoConfiguration类用来处理错误,它给容器中添加了好多组件

1)ErrorPageCustomizer

系统出现错误以后来到ErrorPageCustomizer,会默认发送error请求进行处理

static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
   

	protected ErrorPageCustomizer(ServerProperties properties, DispatcherServletPath dispatcherServletPath) {
   
		this.properties = properties;
		this.dispatcherServletPath = dispatcherServletPath;
	}

	@Override
	public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
   
		ErrorPage errorPage = new ErrorPage(
				this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath()));
				   	//@Value("${error.path:/error}")
                    //private String path = "/error";
		errorPageRegistry.addErrorPages(errorPage);
	}
2)BasicErrorController

用来处理/error错误请求

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
   

    //产生html类型的数据;浏览器发送的请求来到这个方法处理
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
	public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
   
		HttpStatus status = getStatus(request);
		Map<String, Object> model = Collections
				.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
		response.setStatus(status.value());
		
       //去哪个页面作为错误页面;包含页面地址和页面内容
		ModelAndView modelAndView = resolveErrorView(request, response, status, model);
		return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
	}

    //产生json数据,其他客户端来到这个方法处理;
	@RequestMapping
	public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
   
		HttpStatus status = getStatus(request);
		if (status == HttpStatus.NO_CONTENT) {
   
			return new ResponseEntity<>(status);
		}
		Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
		return new ResponseEntity<>(body, status);
	}


}

在处理网页页面的过程中有个resolveErrorView方法,所有的ErrorViewResolver得到ModelAndView

protected ModelAndView resolveErrorView(HttpServletRequest request, HttpServletResponse response, HttpStatus status,
		Map<String, Object> model) {
   
	for (ErrorViewResolver resolver : this.errorViewResolvers) {
   
		ModelAndView modelAndView = resolver.
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Spring Boot和MyBatis-Plus创建登录接口的一般步骤: 1. 创建User实体类并使用注解@Table指定对应的表名和@Column指定对应的字段名。 ```java @Table("user") public class User { @TableId(type = IdType.AUTO) private Long id; @Column("username") private String username; @Column("password") private String password; // 其他属性和方法省略 } ``` 2. 创建UserMapper接口并继承BaseMapper<User>,MyBatis-Plus将会自动生成常用的CRUD方法。 ```java public interface UserMapper extends BaseMapper<User> { } ``` 3. 创建UserService类,实现用户验证的方法,这里以简单的用户名和密码验证为例。 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User login(String username, String password) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("username", username).eq("password", password); return userMapper.selectOne(queryWrapper); } } ``` 4. 创建LoginController类,处理登录请求并调用UserService验证用户信息。 ```java @RestController @RequestMapping("/api") public class LoginController { @Autowired private UserService userService; @PostMapping("/login") public ResponseEntity<String> login(@RequestParam("username") String username, @RequestParam("password") String password) { User user = userService.login(username, password); if (user != null) { // 登录成功,返回token等信息 return ResponseEntity.ok("Login successful"); } else { // 登录失败,返回错误信息 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Incorrect username or password"); } } } ``` 以上就是使用Spring Boot和MyBatis-Plus创建登录接口的基本步骤,需要注意的是,这里只是提供了一个简单的实现示例,实际情况可能还需要进行一些安全性和性能方面的优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值