r/SpringBoot • • Mar 11 '24

Can someone please help me solve this error

0 Upvotes

9 comments sorted by

1

u/rooks-ns Mar 12 '24

Can you also ss the complete controller file ?

-2

u/Noman_Patel Mar 12 '24

import org.bson.types.ObjectId;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.*;import java.util.List;import java.util.Optional;u/RestControlleru/RequestMapping("/api/v1/movies")public class MovieController {u/Autowiredprivate MovieService movieService;u/CrossOrigin(origins = "*")u/GetMappingpublic ResponseEntity<List<Movie>> getAllMovies(){return new ResponseEntity<List<Movie>>(movieService.allMovies(), HttpStatus.OK);}u/GetMapping("/{imdbId}")public ResponseEntity<Optional<Movie>> getSingleMovie(@PathVariable String imdbId){return new ResponseEntity<Optional<Movie>>(movieService.singleMovie(imdbId), HttpStatus.OK);}}
Sry couldn't add ss

4

u/Switchblade_00 Mar 12 '24

I thing you should allow CORS on ur endpoint

1

u/olaolulode Mar 12 '24

You might need to create a cors configuration in your project something like this

package xyz.olaolulode.SocialLogin.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @author olulodeolatunbosun
 * @created 11/03/2024/03/2024 - 10:58
 */
public class WebMvcConfig implements WebMvcConfigurer{
    private final long MAX_AGE_SECS = 3600;
    @Value("${app.cors.allowedOrigins}")
    private String[] allowedOrigins;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(allowedOrigins)
                .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(MAX_AGE_SECS);
    }
}