Saturday, 4 April 2020


2. factorial of a given number'

public long fact(int number)
{
int fact=1;
while(number > 0)
{
fact*=number;
number--;
}
return fact;
}

How To Handle CORS issue (has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs)

I think most of the developers might have seen this issue in their development phase in local environment . we will discuss the solution in this post .

Tech Stack : ReactJS , SpringBoot with Rest

One of the solution worked for me is to add an extend class to WebMvcConfigurerAdapter. 
The below solution will send the allow-access-control-origin as * which will give browser a signal to proceed further . for example if we are accessing the URI from http://localshot:1234 and if we don't implement below class our request won't proceed further because we are not getting Access-Control-Allow-Origin in Response header .

@Configurationpublic class WebConfigurerAdapter extends WebMvcConfigurerAdapter {

    @Override    public void configurePathMatch(PathMatchConfigurer matcher) {
        matcher.setUseRegisteredSuffixPatternMatch(true);    }

    @Override    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/demo/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH")
                .allowCredentials(false).maxAge(3600);    }

    @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    public DispatcherServlet dispatcherServlet() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();        dispatcherServlet.setDispatchOptionsRequest(true);        return dispatcherServlet;    }
}