Springboot2.0记录些区别

最近在搭架子,用了最新的Springboot2.0,发现好多方法都启用了

Posted by if on 2018-09-10
  1. 使用spring mvc解决跨域问题

    之前的解决方案

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Configuration
    public class CORSConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")//设置允许跨域的路径
    .allowedOrigins("*")//设置允许跨域请求的域名
    .allowCredentials(true)//是否允许证书 不再默认开启
    .allowedMethods("GET", "POST", "PUT","PATCH", "DELETE")//设置允许的方法
    .maxAge(3600);//跨域允许时间
    }
    }

    由于WebMvcConfigurerAdapter类过时

    所以使用如下方法实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Configuration
    public class CORSConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")//设置允许跨域的路径
    .allowedOrigins("*")//设置允许跨域请求的域名
    .allowCredentials(true)//是否允许证书 不再默认开启
    .allowedMethods("GET", "POST", "PUT","PATCH", "DELETE")//设置允许的方法
    .maxAge(3600);//跨域允许时间
    }

    }
  2. SpringDataJpa分页接口Pageable的实现类PageRequest构造方法弃用

    old

    1
    new PageRequest(firstResult, maxResults, new Sort(...))

    new

    1
    PageRequest.of(firstResult, maxResults, new Sort(...))