我正在使用Spring Boot创建Web服务。
我正在尝试将HATEOAS引入端点。这是模型(DTO):
public class MovieResponse {
private Long id;
private String name;
private Date releaseDate;
private Time runtime;
private Float rating;
private String storyline;
private String poster;
private String rated;
private Date createdAt;
private List<GenreResponse> genres = new ArrayList<>();
private List<MovieMediaResponse> videos = new ArrayList<>();
private List<MovieMediaResponse> photos = new ArrayList<>();
private List<MovieReviewResponse> reviews = new ArrayList<>();
private List<MovieCelebrityResponse> cast = new ArrayList<>();
private List<MovieCelebrityResponse> writers = new ArrayList<>();
private List<MovieCelebrityResponse> directors = new ArrayList<>();
// Getters & Setters
}
我还有一个PagedResponse
来保存页面信息:
public class PagedResponse<T> {
private List<T> content;
private int page;
private int size;
private long totalElements;
private int totalPages;
private boolean last;
public PagedResponse() {}
public PagedResponse(List<T> content, int page, int size, long totalElements, int totalPages, boolean last) {
this.content = content;
this.page = page;
this.size = size;
this.totalElements = totalElements;
this.totalPages = totalPages;
this.last = last;
}
// Getters & Setters
}
我在服务中也有getAllMovies
方法,该方法返回PagedResponse<MovieResponse>
。
现在是MovieResourceAssembler
@Component
public class MovieResourceAssembler implements ResourceAssembler<MovieResponse, Resource<MovieResponse>> {
private EntityLinks entityLinks;
@Autowired
public void setEntityLinks(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<MovieResponse> toResource(MovieResponse movieResponse) {
Link self = entityLinks.linkFor(MovieResponse.class).slash(movieResponse.getId()).withSelfRel();
Link rel = entityLinks.linkFor(MovieResponse.class).slash(movieResponse.getId()).withRel("movie");
Link movieGenres = entityLinks.linkFor(MovieResponse.class).slash(movieResponse.getId()).slash("movieGenres").withRel("movieGenres");
Link movieReviews = entityLinks.linkFor(MovieResponse.class).slash(movieResponse.getId()).slash("movieReviews").withRel("movieReviews");
return new Resource<>(movieResponse, self, rel, movieGenres, movieReviewsa);
}
}
当我尝试在控制器中使用它时,如下所示:
@RestController
public class MovieController {
private MovieService movieService;
private PagedResourcesAssembler<MovieResponse> pagedAssembler;
private MovieResourceAssembler movieResourceAssembler;
@Autowired
public MovieController(MovieService movieService, PagedResourcesAssembler<MovieResponse> pagedAssembler, MovieResourceAssembler movieResourceAssembler) {
this.movieService = movieService;
this.pagedAssembler = pagedAssembler;
this.movieResourceAssembler = movieResourceAssembler;
}
@GetMapping("/movies")
public ResponseEntity<?> getAllMovies(@RequestParam(value = "page", defaultValue = DEFAULT_PAGE_NUMBER) String page,
@RequestParam(value = "size", defaultValue = DEFAULT_PAGE_SIZE) String size,
@RequestParam(value = "sort", defaultValue = "createdAt") String sort,
@RequestParam(value = "direction", defaultValue = "desc") String direction) {
PagedResponse<MovieResponse> response = this.movieService.getAllMovies(page, size, sort, direction);
return ResponseEntity.ok(this.pagedAssembler.toResource(response, this.movieResourceAssembler));
}
}
遇到以下错误:
无法解析方法'toResource(com.movies.mmdb.util.PagedResponse,com.movies.mmdb.controller.MovieResourceAssembler)'
在此行:return ResponseEntity.ok(this.pagedAssembler.toResource(response, this.movieResourceAssembler));
我认为toResource
在传递org.springframework.data.domain.Page;
的{{1}}变量时接受response
。
我该如何解决?
答案 0 :(得分:0)
如果toResource
方法收到一个Page<T>
作为第一个参数,那么为什么不使用该数据类型呢?如果需要扩展Page,则您的类应实现此类接口以实现方法签名:
public class PagedResponse<T> implements Page<MovieResponse>