我将Spring Boot和MongoDB用于个人项目,以创建电影和视频游戏数据库。我有检索每个类别的最新五个条目的方法。当我在本地Tomcat和MongoDB服务器上进行测试时,这些方法可以按预期工作。但是,当我部署到tomcat服务器并连接到MongoDB服务器时,检索最近五个条目的方法始终返回一个空数组。这是代码:
存储库:
public interface MovieRepository extends MongoRepository<Movies, String>{
public List<Movies> findAllByOrderByTitleAsc(Pageable pageable);
@Query("{'title': {$regex: ?0, $options: 'i'}}")
public List<Movies> findByTitle(String title);
public List<Movies> findTop5ByCreatedAtLessThan(String currentDate);
public Movies findMovieById(String id);
public List<Movies> findByPlatformsIn(List<String> platforms);
}
控制器:
@RestController
@RequestMapping("/api")
public class MovieController {
@Autowired
private MovieRepository movieRepository;
@Autowired
private MapValidationErrorService mapValidationErrorService;
@Autowired
private CurrentDateService currentDateService;
@GetMapping("/movies")
public List<Movies> findAllMovies(@RequestParam(value="page", defaultValue="0")int page) {
return movieRepository.findAllByOrderByTitleAsc(PageRequest.of(page, 20));
}
@GetMapping("/movies/{movieId}")
public Movies findMovieById(@PathVariable("movieId") String movieId) {
return movieRepository.findMovieById(movieId);
}
@PostMapping("/movies")
public ResponseEntity<?> saveMovie(@Valid @RequestBody Movies movie, BindingResult result) {
ResponseEntity<?> errorMap = mapValidationErrorService.mapvalidationService(result);
if (errorMap != null) return errorMap;
movie.setId(null);
Movies newMovie = movieRepository.save(movie);
return new ResponseEntity<Movies>(newMovie, HttpStatus.OK);
}
@PutMapping("/movies")
public ResponseEntity<?> updateMovie(@Valid @RequestBody Movies movie, BindingResult result) {
ResponseEntity<?> errorMap = mapValidationErrorService.mapvalidationService(result);
if (errorMap != null) return errorMap;
Movies updatedMovie = movieRepository.save(movie);
return new ResponseEntity<Movies>(updatedMovie, HttpStatus.OK);
}
@DeleteMapping("/movies/{movieId}")
public void deleteMovieById(@PathVariable("movieId") String movieId) {
movieRepository.deleteById(movieId);
}
@GetMapping("/moviePlatforms")
public List<Movies> findMoviesByPlatforms(@RequestParam(value="platform") List<String>platforms) {
return movieRepository.findByPlatformsIn(platforms);
}
@GetMapping("/newFiveMovies")
public List<Movies> findTop5ByCreatedAt() {
String currentDate = currentDateService.getCurrentDate();
return movieRepository.findByCreatedAt(currentDate);
}
}
我已将find前5种方法更改为使用@Query批注,但结果相同。控制器和存储库中的所有其他方法均按预期工作。我曾尝试在Mac和MongoDB Atlas上使用MongoDB。任何帮助将不胜感激。
答案 0 :(得分:0)
我发现我作为参数发送的日期格式错误。日期被存储为“ M / d / yyh:mm a”,但我发送的是“ M / dd / yyyy hh:mm a”。