• -------------------------------------------------------------
  • ====================================

spring boot web相关配置

springcloud dewbay 5年前 (2019-04-12) 2320次浏览 已收录 1个评论 扫描二维码

 spring boot 集成了 servlet 容器,当我们在 pom 文件中增加springboot-starter-web 的 maven 依赖时,不做任何web相关的配置便能提供web服务,这还得归于spring boot 自动配置的功能(因为加了 EnableAutoConfiguration 的注解),帮我们创建了一堆默认的配置,以前在web.xml 中配置,现在都可以通过spring bean 的方式进行配置,由spring来进行生命周期的管理,大多数情况下,我们需要重载这些配置(例如修改服务的启动端口,contextpath,filter,listener,servlet,session 超时时间等)

      1. servlet 配置

     当应用只有默认的 servlet(即 DispatcherServlet)时,映射的 url 为/,存在其他的 servlet 时,映射的路径为 servlet 的注册的 beanname(可通过@Component 注解修改),创建方式如下:

@Component(“myServlet”)
public class MyServlet implements Servlet{
/**
*
* @see javax.servlet.Servlet#destroy()
/ @Override public void destroy() { System.out.println(“destroy…”); } /*
* @return
* @see javax.servlet.Servlet#getServletConfig()
/ @Override public ServletConfig getServletConfig() { return null; } /*
* @return
* @see javax.servlet.Servlet#getServletInfo()
/ @Override public String getServletInfo() { return null; } /*
* @param arg0
* @throws ServletException
* @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
/ @Override public void init(ServletConfig arg0) throws ServletException { System.out.println(“servlet init…”); } /*
* @param arg0
* @param arg1
* @throws ServletException
* @throws IOException
* @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
*/
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException,
IOException {
System.out.println(“service…”);
}
   
    2. filter 配置

    filter 配置的映射是/*,创建方式如下:

@Component(“myFilter”)
public class MyFilter implements Filter{
/**
*
* @see javax.servlet.Filter#destroy()
/ @Override public void destroy() { System.out.println(“destroy…”); } /*
* @param arg0
* @param arg1
* @param arg2
* @throws IOException
* @throws ServletException
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
/ @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println(“doFilter…”); arg2.doFilter(arg0, arg1); } /*
* @param arg0
* @throws ServletException
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println(“filter init…”);
}

}   
    

    3. listener 配置

@Component(“myListener”)
public class MyListener implements ServletContextListener{
/**
* @param arg0
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
/ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println(“contextDestroyed…”); } /*
* @param arg0
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println(“listener contextInitialized…”);
}

}
如果觉得控制力度不够灵活(例如你想修改 filter 的映射),spring boot还提供了 ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean 这 3 个东西来进行配置
    修改 filter 的映射

@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(MyFilter myFilter){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(myFilter);
filterRegistrationBean.setEnabled(true);
filterRegistrationBean.addUrlPatterns(“/bb”);
return filterRegistrationBean;
}
}

   4. 配置 servlet 容器

    可以通过两种方式配置 servlet 容器,一种是通过 properties 文件,例如:

server.port=8081
server.address=127.0.0.1
server.sessionTimeout=30
server.contextPath=/springboot
完整的配置信息可以看这 http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java
    另一种方式是 java 代码的形式:

@Component
public class MyCustomizationBean implements EmbeddedServletContainerCustomizer {
/**
* @param container
* @see org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer#customize(org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer)
*/
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setContextPath(“/sprintboot”);
container.setPort(8081);
container.setSessionTimeout(30);
}

}
   5. error 处理
   spring boot 提供了/error 映射来进行错误处理,它会返回一个 json 来对错误信息进行描述(包含了 http 状态和异常信息),例如 404 的错误

     当然可以定制错误页面,通过实现 ErrorController 接口,并将它装配起来即可,如下:     

@Controller
public class ErrorHandleController implements ErrorController {
/**
* @return
* @see org.springframework.boot.autoconfigure.web.ErrorController#getErrorPath()
*/
@Override
public String getErrorPath() {
return “/screen/error”;
}

@RequestMapping
public String errorHandle(){
    return getErrorPath();
}

}
    还可以这样:

@Component
private class MyCustomizer implements EmbeddedServletContainerCustomizer {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
    container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/screen/error"));
}

}
   6.模板引擎
    spring boot 会自动配置 FreeMarker,Thymeleaf,Velocity,只需要在 pom 中加入相应的依赖即可,例如应用 Velocity   

org.springframework.boot spring-boot-starter-velocity
    默认配置下 spring boot 会从 src/main/resources/templates 目录中去找模板
  7. jsp 限制

    如果要在 spring boot 中使用 jsp,得将应用打包成 war,这里有配置的 example https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp


露水湾 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring boot web相关配置
喜欢 (0)
[]
分享 (0)
关于作者:
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 士大夫
    dewbay2020-10-23 15:53 回复 Windows 10 | Chrome 86.0.4240.75