There was an unexpected error (type=Internal Server Error, status=500).
Command failed with error 2 (BadValue): 'Field ‘locale’ is invalid in: { locale: “movies” }
What command were you executing to result in this error?
I was running spring boot application
And what was the code you executed to throw this error? Do you have schema validation enabled, and if so is it set to strict?
What is the exact code you’re running, if you give more details it’s easier for someone to see what could be the issue.
MoviesApplication file:
package dev.harsh.movies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MoviesApplication {
public static void main(String[] args) {
SpringApplication.run(MoviesApplication.class, args);
}
}
MovieService file:
package dev.harsh.movies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MovieService {
@Autowired
private MovieRepository movieRepository;
public List<Movie> allMovies(){
return movieRepository.findAll();
}
}
MovieRepository file:
package dev.harsh.movies;
import org.bson.codecs.ObjectIdCodec;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MovieRepository extends MongoRepository<Movie, ObjectIdCodec> {
}
MovieController
package dev.harsh.movies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.List;
@RestController
@RequestMapping("api/v1/movies")
public class MovieController {
@Autowired
private MovieService movieService;
@GetMapping
public ResponseEntity<List<Movie>> allMovies(){
return new ResponseEntity<List<Movie>>(movieService.allMovies(), HttpStatus.OK);
}
}
Movie
package dev.harsh.movies;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.annotation.Id;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import java.util.List;
@Document(collation = "movies")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Movie {
private ObjectId id;
private String imdbId;
private String title;
private String releaseDate;
private String trailerLink;
private String poster;
private List<String> genres;
private List<String> backdrops;
@DocumentReference
private List<Review> reviewIds;
}
Spring / Java is not my speciality but do the documents in the collection have the locale field but the class definition it’s trying to map the data to does not?
Use @Document(collection = “movies”) instead @Document(collation = “movies”)
I got the same
Good spot, I missed that!
Brother, you are a lifesaver, I am new to everything, and I have no clue of what I am doing. I cannot believe there was a person with the exact specific problem as me and someone else who figured it out too. Thanks so much for the solution, really appreciate it.
Take it easy bro
Bro Thanks a million for your amazing support
thanks a lot, you save me
Dammned to see so many folks getting same problem Yall doing freeCodeCamp course too?