栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot框架学习理解下

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot框架学习理解下

  • 通过@ControllerAdvice与@ExceptionHandler注解处理异常
    注意@ExceptionHandler注解它只能处理的异常范围在本控制类中,这就说明想要处理其他控制类中的异常是不可能的,只能是每个类中添加处理异常的方法,但是有一个种处理异常类可以是处理全局的异常,

  • 创建全局异常处理类

1 编写报错的控制类
 //测试异常类 会报错的方法
    @RequestMapping("showexcetpion")
    public String showexception(){
        int a = 10/0;
        return "夏末自习室";
    }
  ===================
  2 创建一个全局处理异常类
  package com.zixue.springbootmybatis.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;


@ControllerAdvice
public class GlobalException {
    //进行处理nullpointer异常的控制类,会处理全局的异常
    //这个注解的作用就是根据触发的异常跳转到相对应的视图,
    @ExceptionHandler(value = {java.lang.NullPointerException.class})
    public ModelAndView nullException(Exception e){
        //创建一个页面视图
        ModelAndView mav = new ModelAndView();
        //视图存放异常信息
        mav.addObject("err",e.toString());
        //设置异常处理页面视图名称
        mav.setViewName("error");

        return mav;

    }
    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
    public ModelAndView Mathexception(Exception e){
        //创建一个页面视图
        ModelAndView mav = new ModelAndView();
        //视图存放异常信息
        mav.addObject("err",e.toString());
        //设置异常处理页面视图名称
        mav.setViewName("error");
        mav.addObject("exception","是算术异常");

        return mav;

    }
}
============
页面
 
  • 通过SimpleMappingExceptionResolver对象来处理异常
    使用@ControllerAdvice与@ExceptionHandler处理全局异常很方便但是注意每一个异常都要写一个方法,意味着这个异常全局类会有很多的方法,
    可以使用SimpleMappingExceptionResolver对象来定义每一个异常跳转到哪个页面中,这样就在一个方法中搞定了
1创建全局异常处理类
package com.zixue.springbootmybatis.exception;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.Properties;


@Configuration
public class GlobalException2 {

    //此方法必须是返回SimpleMappingExceptionResolver对象
    @Bean
    public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        //这个对象就是用来将异常和视图做一个映射的作用
        //创建一个properties对象来存放异常和页面
        Properties properties = new Properties();
        properties.put("java.lang.NullPointerException.class","error");
        properties.put("java.lang.ArithmeticException.class","errortwo");
        simpleMappingExceptionResolver.setExceptionMappings(properties);
        return simpleMappingExceptionResolver;
    }
}

=========
异常页面



    
    错误页面


        报空指针异常了


==========



    
    错误页面


    报算术异常了



  • 自定义全局异常处理类
    已经有了SimpleMappingExceptionResolver为什么还要自定义handlerExceptionResolver呢?
    因为SimpleMappingExceptionResolver这个不能传递异常信息,所有要自定义一个异常类来传递异常信息。在显示出来啊
1自定义一个全局异常处理类
package com.zixue.springbootmybatis.exception;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Configuration
public class GlobalException3 implements HandlerExceptionResolver {

    
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        //创建一个视图对象
        ModelAndView mav = new ModelAndView();
        //判断不同异常类型 做不同的视图跳转
        // instanceof 是java保留关键字 作用是测试左边的对象是否是右边类的实例,返回是boolean类型
        if(ex instanceof NullPointerException){
            mav.setViewName("error");
        }if (ex instanceof ArithmeticException){
            mav.setViewName("errortwo");
        }
        //给视图里面存放异常信息啊
        mav.addObject("errorinfo",ex.toString());
        return mav;
    }
}

===============
2 创建页面哈



    
    错误页面


    报算术异常了
    


  • springboot整合测试单元Junit
1 添加依赖

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
============
2 进行测试类
package com.zixue.springbootmybatis;
import com.zixue.springbootmybatis.Service.EmployeeService;
import com.zixue.springbootmybatis.pojo.Employee;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.sql.SQLException;
import java.util.List;

@SpringBootTest(classes = SpringbootmybatisApplication.class)
class SpringbootmybatisApplicationTests {
    @Autowired
    private EmployeeService employeeService;

    @Test
    void contextLoads() throws SQLException {
        List selectinfo = this.employeeService.selectinfo();
        selectinfo.stream().forEach(System.out::println);
    }

}

  • springboot实现热部署
    1 通过DevTools工具实现热部署
1添加依赖
 
        
            org.springframework.boot
            spring-boot-devtools
            true
        

2配置idea 设置自动编译

ctrl + shift+alt+/

  • springbootadmin可视化监控应用
    1 使用sprinboot admin server 需要建立服务端与客户端
    服务端:就是可视化监控的应用,
    客户端:被监控的项目,
    关系:一个服务端可以监控多个客户端,
    它是一个第三方应用,
    2 搭建监听服务端应用
    注意目前spring boot admin Starter Server 2.1.6版本不支持spring boot2.2.x 版本只能支持2.1. x
    所有要使用spring boot 2.1.x版本来搭建服务端项目
1 创建项目boot 添加依赖 


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.10.RELEASE
         
    
    com.example
    springbootservice
    0.0.1-SNAPSHOT
    springbootservice
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            de.codecentric
            spring-boot-admin-starter-server
            2.1.6
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2 配置文件 
server.port=8089
3 开启服务端应用
package com.example.springbootservice;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer//开启springboot admin 服务器
public class SpringbootserviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootserviceApplication.class, args);
    }

}


启动成功!!!

  • springboot打包方式
    1 添加打包插件,boot自带打包插件
 
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        

这个插件的作用是在打包项目时会把项目依赖的jar包也一起打包进来,如果没有这个依赖插件,项目打包后中没有boot依赖的包,
2 manve打包 双击install

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/389972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号