我正在尝试创建我的第一个 Spring Boot +CrudRepository + RestAPI +MySSQL。在最初的打嗝之后,它正在从数据库中插入和获取记录。但是,当我发送自定义查询时,它不起作用。我没有得到任何异常,但是在 Postman 中我得到 404 not found 状态。
这是我的实体类
package com.it.skill.movies;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="movie")
public class Movie {
@Id
@Column(name="mno")
private Integer mno;
@Column(name="mname")
private String mname;
@Column(name="rating")
private Integer rating;
@Column(name="genre")
private String genre;
public Movie() {}
public Movie(Integer mno, String mname, Integer rating, String genre) {
super();
this.mno = mno;
this.mname = mname;
this.rating = rating;
this.genre = genre;
}
public Integer getMno() {
return mno;
}
public void setMno(Integer mno) {
this.mno = mno;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
@Override
public String toString() {
return "Movie [mno=" + mno + ", mname=" + mname + ", rating=" + rating + ", genre=" + genre + "]";
}
}
存储库接口:这是我编写查询的地方。是否有替代方法,因为它是一个非常简单的查询?
package com.it.skill.repo;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.it.skill.movies.Movie;
@Repository
public interface IMovieRepo extends CrudRepository<Movie, Integer>{
@Query("SELECT m FROM Movie m where m.genre=?1")
public List<Movie> findByGenre(String genre);
}
服务等级
package com.it.skill.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.it.skill.movies.Movie;
import com.it.skill.repo.IMovieRepo;
@Service
public class IMovieService {
@Autowired
private IMovieRepo movieRepository;
public Integer addMovie(Movie movie)
{
//Movie movie = new Movie();
movieRepository.save(movie);
return movie.getMno();
}
public List<Integer> addMovies(List<Movie> lstMovie)
{
//Movie movie = new Movie();
List<Integer> lstRating = new ArrayList();
for(Movie movie : lstMovie)
{
movieRepository.save(movie);
lstRating.add(movie.getMno());
}
return lstRating;
}
public List<Movie> getAllMovies()
{
List<Movie> lstMovie = new ArrayList<Movie>();
movieRepository.findAll().forEach(e->lstMovie.add(e));
return lstMovie;
}
public List<Movie> findMoviesByGenre(String genre)
{
List<Movie> lstMovie = new ArrayList<Movie>();
movieRepository.findByGenre(genre);
return lstMovie;
}
}
控制器类:
package com.it.skill.controller;
import java.util.List;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.it.skill.movies.Movie;
import com.it.skill.service.IMovieService;
@RestController
public class MovieController {
@Autowired
private IMovieService movieService;
//add single project
@PostMapping(path = "/movie/add/", consumes = "application/json")
public ResponseEntity<String> addMovie(@RequestBody Movie movie) {
Integer iRating = movieService.addMovie(movie);
if(iRating!=null)
{
return new ResponseEntity<String>("Movie saved with rating: " +iRating, HttpStatus.CREATED);
}
return new ResponseEntity<String>("Movie not saved", HttpStatus.NOT_ACCEPTABLE);
}
@PostMapping(path = "/movies/add/", consumes = "application/json")
public ResponseEntity<List<Integer>> addMovies(@RequestBody List<Movie> lsMovie) {
List<Integer> lsRating = movieService.addMovies(lsMovie);
if(!lsRating.isEmpty())
{
return new ResponseEntity<List<Integer>>(lsRating, HttpStatus.CREATED);
}
return new ResponseEntity<List<Integer>>(HttpStatus.NOT_ACCEPTABLE);
}
@GetMapping(path = "/movies/", produces = { "application/json", "application/xml" })
public ResponseEntity<List<Movie>> getAllMovies() {
List<Movie> lsMovies = movieService.getAllMovies();
if(!lsMovies.isEmpty())
{
return new ResponseEntity<List<Movie>>(lsMovies, HttpStatus.FOUND);
}
return new ResponseEntity<List<Movie>>(HttpStatus.NOT_FOUND);
}
@GetMapping(path = "/movies/bygenre/{genre}", produces = { "application/json", "application/xml" })
public ResponseEntity<List<Movie>> getMoviesByGenre(@PathVariable("genre") String genre) {
List<Movie> lsMovies = movieService.findMoviesByGenre(genre);
if(!lsMovies.isEmpty())
{
return new ResponseEntity<List<Movie>>(lsMovies, HttpStatus.FOUND);
}
return new ResponseEntity<List<Movie>>(HttpStatus.NOT_FOUND);
}
}
以下是应用程序属性
spring.application.name=Restful-App-Movies
server.servlet.context-path=/Restful-App-Movies
server.port=8082
## MySQL
spring.datasource.url=jdbc:mysql://localhost:3306/sakila
spring.datasource.username=root
spring.datasource.password=Asand9876@
当我使用 http://localhost:8082/Restful-App-Movies/movies/bygenre/"Horror" 在邮递员中使用 GET 方法时,此链接会引发 404 not found 错误。
为什么 CrudRepository 接口中的以下方法不能按预期工作?@Query("SELECT m FROM Movie m where m.genre=?1") public List findByGenre(String Genre);