content: package br.com.alevhvm.adotai.common.config;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.MediaType;
  import org.springframework.http.converter.HttpMessageConverter;
  import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
  import org.springframework.web.servlet.config.annotation.CorsRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  
  import br.com.alevhvm.adotai.common.converter.YamlJackson2HttpMessageConverter;
  
  @Configuration
  public class WebConfig implements WebMvcConfigurer {
  
      private static final MediaType MEDIA_TYPE_APPLICATION_YML = MediaType.valueOf("application/yaml");
  
      //colocar o dominio do front para este estar liberado a receber a nossa api
      @Value("${cors.originPatterns:default}")
      private String corsOriginPatterns = "";
  
      @Override
      public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
          converters.add(new YamlJackson2HttpMessageConverter());
      }
  
      @Override
      public void addCorsMappings(CorsRegistry registry) {
          var allowedOrigins = corsOriginPatterns.split(",");
          registry.addMapping("/**")
                  .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
                  .allowedHeaders(
                          "Content-Type",
                          "Authorization",
                          "Accept",
                          "X-Requested-With")
                  .allowedOriginPatterns(allowedOrigins)
                  .allowCredentials(true);
      }
  
      @Override
      public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
          // Via HEADER PARAM. http://localhost:8080/api/usuario/v1
          configurer.favorParameter(false)
                  .ignoreAcceptHeader(false)
                  .useRegisteredExtensionsOnly(false)
                  .defaultContentType(MediaType.APPLICATION_JSON)
                  .mediaType("json", MediaType.APPLICATION_JSON)
                  .mediaType("xml", MediaType.APPLICATION_XML)
                  .mediaType("x-yaml", MEDIA_TYPE_APPLICATION_YML);
      }
  }