Maven Java 17 and 18 Spring Boot Cors not working

Viewed 485

I am migrating from Java 11 to Java 17 with Spring Boot MyBatis Postgresql. I try java 18 and its the same. I change the java version in pom as bellow.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rostech</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>api</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>17</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.2</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
             </dependency>
 <dependency>
  <groupId>com.vaadin.external.google</groupId>
  <artifactId>android-json</artifactId>
  <version>0.0.20131108.vaadin1</version>
  <type>jar</type>
 </dependency>
</dependencies>
<build>
            <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

with cors setting in my main.

public static void main(String[] args) {
    SpringApplication.run(ApiApplication.class, args);
}

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                                    .allowedOrigins("*")
                                    .allowedHeaders("*")
                                    .allowedMethods("GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS");
        }
    };
}

Will get this error. About CORS and I didnt change anything.

Access to XMLHttpRequest at 'https://*server*:8443/api/getHargaByName/be' from origin 'https://*server*' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

And my controller

@RestController
public class GreetingController {
    private final PlatformTransactionManager transactionManager;

    public GreetingController(ArticleMapper articleMapper ,PlatformTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
        this.articleMapper = articleMapper;
    }
    @Autowired
    private final ArticleMapper articleMapper;
    @GetMapping(path+"/getHargaByName/{name}")
    public ArrayList<Pembelian> getHargaByName(@PathVariable(name = "name", required = false) String name) {
        ArrayList<Pembelian>  hasil = new ArrayList<>();
        try {
            if(null == name || name.isBlank()){
                name = "%%";
            } else {
                name = "%"+name+"%";
            }
            hasil = this.articleMapper.getHarga(name);
            
        } catch (Exception ex) {
            printStackTrace(ex);
        }
        return hasil;
    }
}

But if I change to java 11 everything is fine. Is there new way to config CORS?

I tried with a new spring boot from https://start.spring.io/ with java 17 and 18 with spring web, mybatis and postgressql. On run it gives error as bellow.

fresh spring boot java 18

Edit For the "}" was un expected at this time. I have to edit the mvnw.cmd file. From :

powershell -Command "&{"^
    "$webclient = new-object System.Net.WebClient;"^
    "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
    "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
    "}"^
    "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
    "}"

To :

powershell -Command "&{"^
    "$webclient = new-object System.Net.WebClient;"^
    "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
    "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
    "}"^
    "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%');"^
0 Answers
Related