SpringBoot入门篇(二)

本文主要介绍了SpringBoot的web开发,包括webJar的使用、servlet、filter、intercept和listener的集成,以及静态资源映射和异常处理。详细讲述了如何注册自定义servlet、filter和interceptor,并分析了静态资源映射的原理。同时,讨论了SpringBoot的异常处理,包括默认错误页面和自定义错误信息的方法。最后提到了日志处理。
摘要由CSDN通过智能技术生成

今天的博客主题

       SpringBoot ——》SpringBoot入门篇(二)


springboot可以在做成一个独立的项目运行,也可以做成前后端分离的那种。

做成一个独立的项目,在工程里要写前端的代码(HTML,CSS,JS)

如果是前后端分离,那工程里只需要提供接口出去就好。

 

springboot的web开发

1)webJar

webJar就是以jar包的形式来引入前端资源,比如Jquery,FreeMarker,Thymeleaf等等

2)引入对应的JAR包

在pom引入需要的前端资源jar包

<!-- 引入Jquery依赖包 -->
<dependency>
    <groupId>org.webjars.bower</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.0</version>
</dependency>

3)映射规则

所有/webJars/** 都会被映射到/META-INF/resources/webjars/目录下进行处理

4)原理剖析

请求url:http://localhost:8080/webjars/jquery/3.3.0/dist/jquery.js

# 启动日志里的一段代码
Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
# 请求日志里的一句代码
Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]

通过日志分析出,在启动的时候,进行了资源处理程序映射

核心处理方法:WebMvcAutoConfiguration # addResourceHandlers

 

 

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   if (!this.resourceProperties.isAddMappings()) {
      logger.debug("Default resource handling disabled");
      return;
   }
   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
   // 处理映射webJars请求
   if (!registry.hasMappingForPattern("/webjars/**")) {
      customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
   // 处理静态资源文件
   String staticPathPattern = this.mvcProperties.getStaticPathPattern();
   if (!registry.hasMappingForPattern(staticPathPattern)) {
      customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
            .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
   }
}

springboot集成servlet,filter,intercept,listeners

在springboot上扩展springMvc的配置。

在众多的web应用开发中,少不了一些拦截器,过滤器,监听什么的。

1)注册servlet

创建一个自定义的servlet

package com.springboot.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: SpringbootServlet
 * @Description: 自定义servlet
 * @Author: uziJamesi
 * @Date 2020/2/5 12:06
 * ...
 */
public class SpringbootServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException{
        doPost(req,resp);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("自定义servlet#doPost");
        resp.getWriter().write("Hello Servlet");
    }
}

注册到 WebMvcConfigurerAdapter 。新建配置类,继承 WebMvcConfigurerAdapter

package com.springboot.config;

import com.springboot.servlet.SpringbootServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @ClassName: SpringbootConfig
 * @Description: web工程配置
 * @Author: uziJamesi
 * @Date 2020/2/5 10:54
 * ...
 */
@Configuration
public class SpringbootConfig extends WebMvcConfigurerAdapter {
    /**
     * 注册一个servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean addServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new SpringbootServlet());
        servletRegistrationBean.addUrlMappings("/*");
        return servletRegistrationBean;
    }
}

启动项目,测试,发起请求。servlet成功执行...

2)注册filter

创建一个自定义的过滤器

package com.springboot.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @ClassName: SpringbootFilter
 * @Description: 自定义过滤器
 * @Author: uziJamesi
 * @Date 2020/2/5 11:53
 * ...
 */
public class SpringbootFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletExceptio
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值