0

我正在尝试创建我的第一个 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);

4

5 回答 5

0
  1. 您的 Movie 构造函数中不需要 super()
  2. 下面的噪音要小得多 - 更容易阅读
    public List<Movie> findMoviesByGenre(String genre) {
        return movieRepository.findByGenre(genre);
    }
于 2021-10-28T15:01:19.640 回答
0

我在服务课上犯了错误。我声明了列表对象但没有返回列表。当我更新我的服务方法时,它就像魅力一样。这是更新的方法。

public List<Movie> findMoviesByGenre(String genre)
    {
        
        List<Movie> lstMovie = new ArrayList<Movie>();
        lstMovie = movieRepository.findByGenre(genre);
        return lstMovie;
    }
于 2021-06-11T13:27:28.727 回答
0

您应该从请求中删除双引号以使其正常工作。正确的语法应该是:

http://localhost:8082/Restful-App-Movies/movies/bygenre/Horror
于 2021-06-11T11:27:35.717 回答
0

在 addMovies() 函数上方添加 @Transactional 注解

于 2021-06-11T11:00:43.403 回答
0

添加@RequestMapping上面的@RestController注释。所以它变成

@RequestMapping
@RestController
public class MovieController {
....
}
于 2021-06-12T15:16:15.673 回答