我使用了SpringBoot、Maven和PostgreSQL JDBC来创建一个简单的电影API,但每当我发出http GET请求时,这个错误总是出现:
{
我不知道为什么
我的控制器类:
package com.br.Idespair.MoviesAPIWithSQL.controller;
import com.br.Idespair.MoviesAPIWithSQL.model.Movie;
import com.br.Idespair.MoviesAPIWithSQL.service.MovieService;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("api/movies")
public class MovieController {
@Autowired
private MovieService movieService;
@GetMapping
public ResponseEntity<List<Movie>> getAllMovies(){
return new ResponseEntity<List<Movie>>
(movieService.AllMovies(), HttpStatus.OK);
}
@GetMapping("/{imdbId}")
public ResponseEntity<Optional<Movie>>
getSingleMovie(@PathVariable
String imdbId){
return new ResponseEntity<Optional<Movie>>
(movieService.singleMovie(imdbId),HttpStatus.OK);
}
}
我的服务类别:
package com.br.Idespair.MoviesAPIWithSQL.service;
import com.br.Idespair.MoviesAPIWithSQL.model.Movie;
import
com.br.Idespair.MoviesAPIWithSQL.repository.IMovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class MovieService {
@Autowired
private IMovieRepository movieRepository;
public List<Movie> AllMovies(){
return movieRepository.findAll();
}
public Optional<Movie> singleMovie(String imdbId){
return movieRepository.findMovieByImdbId(imdbId);
}
}
我在其中创建电影实例的模型类:
package com.br.Idespair.MoviesAPIWithSQL.model;
import jakarta.persistence.*;
;
import lombok.Data;
import org.springframework.data.annotation.Reference;
import java.util.List;
@Entity
@Data
@Table(name = "movies")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String imdbId;
private String title;
private String releaseDate;
private String trailerLink;
private String poster;
private List<String> genres;
private List<String> backdrops;
@OneToMany(mappedBy = "movie")
private List<Review> reviews;
public Movie(String imdbId, String title, String
releaseDate, String trailerLink, String poster, List<String>
genres, List<String> backdrops) {
this.imdbId = imdbId;
this.title = title;
this.releaseDate = releaseDate;
this.trailerLink = trailerLink;
this.poster = poster;
this.genres = genres;
this.backdrops = backdrops;
}
}
和我的存储库类:
package com.br.Idespair.MoviesAPIWithSQL.repository;
import com.br.Idespair.MoviesAPIWithSQL.model.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface IMovieRepository extends
JpaRepository<Movie,String> {
Optional<Movie> findMovieByImdbId(String imdbId);
}
我试图读取错误消息,它告诉我对象imdb_id有错误,应该是imDBID,但所有引用imDBID的对象都是以这种方式写入的,并且我的任何类上都没有写入imdb_id.
我使用postman作为HTTP请求,使用postgres作为数据库 我还没有在这个应用程序上使用Docker.