#Error: no CORS header. After switching to java 23
1 messages ยท Page 1 of 1 (latest)
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>.
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.
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:
is the Origin header present in the request?
This is unrelated to Java 23.
i assume so, this is the full React error message: Access to XMLHttpRequest at 'http://localhost:8080/api/memail/email_inbox' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Try adding @Bean to the CorsConfig method. and you may need to add the corsConfigurationSource bean too. However, or normally, the WebMvcConfigurer should be it's own config class that's also annotated with @EnableWebMvc
Thanks, ive tryed. Stll cant connect.
Try using registry.addMapping("/api/**").allowedOrigins("http://localhost:5173")
same result
you mean it has nothing to do with me switching versions?
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?
Correct.
Wierd
did you go 22 -> 23 or 8 -> 23
17 to 23 also changed the JAVA_HOME env variable
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
I switched trough all even 17 and all have the issue
okay so it sounds unrelated to the version
you changed java home
maybe java home wasn't on 17 before
and its a build tool thing
Yes i tryed setting that back to 17, but it seems like idea doesn't use that env variable
or, did you upgrade spring versions too?
you need to fully restart intellij for it to pick up env vars
Yes spring 3.3.4 to 3.3.5 and switched back now
@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 ๐
Not resolved
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
@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 ๐
No ideas?
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
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
right, i see
and it broke during an upgrade, which hints to me the problem is in the magic juice, not your code
recent upgrade?
i mean, you said it happened when you switched to 23
yeah but ive tryed going back to 17 also
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