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

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

posted @ 2020-05-08 15:48  关键的疯狂  阅读(283)  评论(0编辑  收藏  举报