4

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.

1
  • 3
    What have you tried and what is/are the problem(s) you have encountered? Commented Jun 6, 2018 at 23:53

1 Answer 1

4

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

public WebMvcConfigurer corsConfigurer() {
     return registry -> registry.addMapping("/**")
                                .allowedOrigins("*")
                                .allowedMethods("*");
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Jacob. This produced: [ERROR] incompatible types: org.springframework.web.servlet.config.annotation.WebMvcConfigurer is not a functional interface no abstract method found in interface org.springframework.web.servlet.config.annotation.WebMvcConfigurer
Yep, this only works if WebMvcConfigurer is a functional interface, which it apparently isn't. The only other option is to create your own functional interface that extends WebMvcConfigurer.
This may be a bug in sonar. Looking at WebMvcConfigurer, all of its methods are default. It could be that sonar thinks it's a functional interface because it doesn't have more than one abstract method; it should instead be checking that it had exactly one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.