#Error: no CORS header. After switching to java 23

1 messages ยท Page 1 of 1 (latest)

vale citrusBOT
#

<@&1004656351647117403> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
After switching to java 23 i cant connect to spring, no CORS headers

When you encounter the error "no CORS header" after switching to Java 23 in your Spring application, it means that your server is not sending the required Cross-Origin Resource Sharing (CORS) headers. This issue typically arises when trying to make cross-origin requests from a frontend application to a backend server.

To resolve this problem, you can configure CORS in your Spring application by adding the necessary headers to allow cross-origin requests. You can do this by either using Java configuration or annotations.

If you prefer Java configuration, you can create a configuration class that extends WebMvcConfigurerAdapter and override the addCorsMappings method to define the CORS configuration.

Alternatively, you can use annotations by adding @CrossOrigin annotation at the controller level or method level to specify which origins are allowed to access your endpoints.

By setting up CORS properly in your Spring application, you should be able to resolve the "no CORS header" error and successfully connect to your backend server. Make sure to test your frontend application after making these changes to ensure that cross-origin requests are working as expected.

wintry plover
#

Ive tryed both the @CrossOrigin annotation on my controller classes and ice tryed the global way by adding a WebMvcConfigurer to my main class

#
@SpringBootApplication
public class MBlogApplication {
    public static void main(String[] args) {
        SpringApplication.run(MBlogApplication.class, args);
    }
    public WebMvcConfigurer CORSConfig() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api").allowedOrigins("http://localhost:5173");
            }
        };
    }
}

The main class way:

nova nest
#

is the Origin header present in the request?

fierce gust
#

This is unrelated to Java 23.

wintry plover
static lintel
wintry plover
static lintel
#

Try using registry.addMapping("/api/**").allowedOrigins("http://localhost:5173")

wintry plover
#

same result

wintry plover
#

should my old method of just having @CrossOrigin annotation on the controller classes not work still? is that deprecated?

#

or am i looking in the comletely wrong place for this bug?

wintry plover
#

Wierd

grave ginkgo
wintry plover
grave ginkgo
#

well, try 17 -> 21

#

then if that doesn't work 17 -> 19

#

and if that works 19 -> 20

#

etc.

#

lets find the exact version where the problem shows up

wintry plover
#

I switched trough all even 17 and all have the issue

grave ginkgo
#

okay so it sounds unrelated to the version

wintry plover
#

Yup

#

But i dont remember changeing anything else

grave ginkgo
#

you changed java home

#

maybe java home wasn't on 17 before

#

and its a build tool thing

wintry plover
#

Yes i tryed setting that back to 17, but it seems like idea doesn't use that env variable

grave ginkgo
#

or, did you upgrade spring versions too?

#

you need to fully restart intellij for it to pick up env vars

wintry plover
#

Yes spring 3.3.4 to 3.3.5 and switched back now

vale citrusBOT
#

@wintry plover

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘

wintry plover
#

Not resolved

static lintel
#

Can you send the code for your CorsConfiguration?

wintry plover
# static lintel Can you send the code for your CorsConfiguration?
package org.mm.MBlog;

import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
public class CorsConfig {
    @Bean
    public WebMvcConfigurer CORSConfig() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**").allowedOrigins("http://localhost:5173");
            }
        };
    }
}

the same problem here is also present if i try to instead do @CrossOrigin annotation on the controller classes also

#

this is the full code if your interested, altough i assume without idea`s build settings, not sure how to show those

vale citrusBOT
#

@wintry plover

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘

wintry plover
#

No ideas?

grave ginkgo
#

not really - my approach would be to have your project on my machine and start fiddling

#

but i don't have time for that right now

wintry plover
#

You havent had a cors issue like this by chance?

#

Thanks for your time though

grave ginkgo
#

CORS is a little bit of a nightmare every time

#

i can tell you exactly how it needs to work at the HTTP layer

#

its actually pretty straight forward there - on a head request, respond back with a few special headers that let the browser know its okay to make a cross origin request

#

but we're like 4 layers deep in spring magic

wintry plover
#

right, i see

grave ginkgo
#

and it broke during an upgrade, which hints to me the problem is in the magic juice, not your code

wintry plover
#

recent upgrade?

grave ginkgo
#

i mean, you said it happened when you switched to 23

wintry plover
#

yeah but ive tryed going back to 17 also

grave ginkgo
#

maybe it was a different thing you changed that you didn't think about

#

now, i will suggest

#

if this is a personal or school project

#

the reason you added CORS is likely to support a React/Vue/whatever javascript frontend

#

you can skip this whole nightmare if you just render html on the server

wintry plover
#

Yup react with axios library

#

Ahh well the fun part was making a project that uses both java and react heavely ๐Ÿ˜„