Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert anonymous inner class to Java 8 lambda

Sonar static code analysis tells me this is a code smell and should be converted to a lamdba.

 public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
               .allowedOrigins("*")
               .allowedMethods("*");
            }
        };
    }

I've tried various approaches with no luck. Any help is appreciated.

like image 595
Justin James Avatar asked Sep 19 '25 13:09

Justin James


1 Answers

Assuming WebMvcConfigurer is a functional interface, the method can be changed to the following:

public WebMvcConfigurer corsConfigurer() {
     return registry -> registry.addMapping("/**")
                                .allowedOrigins("*")
                                .allowedMethods("*");
}
like image 128
Jacob G. Avatar answered Sep 21 '25 02:09

Jacob G.