• spring boot Java配置搭建ssm (二)


    1、配置静态资源过滤,SpringMVCConfig.java 继承 WebMvcConfigurationSupport 

    @Configuration
    @ComponentScan(basePackages = "org.javaboy",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type =
            FilterType.ANNOTATION,classes = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,classes
            = Configuration.class)} )
    public class SpringMVCConfig extends WebMvcConfigurationSupport {
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        }
    }

    在 resources 文件下创建一个 static文件,在 static文件中创建一个 hello.html ,启动Tomcat后,访问http://localhost:8080/static/hello.html

    2、配置拦截器,先创建 MyInterceptor 类,并实现 HandlerInterceptor 接口

    public class MyInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("postHandle");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("afterCompletion");
        }
    }

    SpringMVCConfig.java 继承 WebMvcConfigurationSupport 

    @Configuration
    @ComponentScan(basePackages = "org.javaboy",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type =
            FilterType.ANNOTATION,classes = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,classes
            = Configuration.class)} )
    public class SpringMVCConfig extends WebMvcConfigurationSupport { @Override
        protected void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
        }
    
        @Bean
        MyInterceptor myInterceptor(){
            return new MyInterceptor();
        }
    }

    启动Tomcat,访问http://localhost:8080/hello

    控制台输出

    preHandle
    postHandle
    afterCompletion

    3、配置fastjson

    @Configuration
    @ComponentScan(basePackages = "org.javaboy",useDefaultFilters = false,includeFilters = {@ComponentScan.Filter(type =
            FilterType.ANNOTATION,classes = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,classes
            = Configuration.class)} )
    public class SpringMVCConfig extends WebMvcConfigurationSupport {
        @Override
        protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
            converters.add(converter);
        }
    }

    pom.xml 添加依赖

      <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.58</version>
            </dependency>

    创建controller

    @RestController
    public class HelloController {
    
        @GetMapping("/data")
        public List<String> getData(){
            ArrayList<String> list = new ArrayList<>();
            for (int i=0;i<10;i++){
                list.add("www.org.javaboy>>>"+i);
            }
            return list;
        }
    }

    启动Tomcat,访问http://localhost:8080/data

    此文章是通过个人搬砖汇总所得,用于个人复习。如有错误,麻烦指出,谢谢!

  • 相关阅读:
    XML指南——XML 瀏覽器(Netscape、Explorer)
    XML指南——察看 XML 文件
    ASP中Dictionary的使用
    SQL Mobile的RDA数据同步开发
    XML指南——XML 確認
    简单的js分页脚本
    浏览器语种检测,适合于多语言版本的站点
    Com/Dcom/Com+的思考
    XML指南——XML數據島
    MOSS 2007 功能概述
  • 原文地址:https://www.cnblogs.com/wdss/p/12850917.html
Copyright © 2020-2023  润新知