如何在自定义页面上使用HATEOAS

时间:2018-09-17 19:06:55

标签: java spring spring-boot spring-data-jpa spring-hateoas

我正在使用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.movi​​es.mmdb.util.PagedResponse,com.movi​​es.mmdb.controller.MovieResourceAssembler)'

在此行:return ResponseEntity.ok(this.pagedAssembler.toResource(response, this.movieResourceAssembler));

我认为toResource在传递org.springframework.data.domain.Page;的{​​{1}}变量时接受response

我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果toResource方法收到一个Page<T>作为第一个参数,那么为什么不使用该数据类型呢?如果需要扩展Page,则您的类应实现此类接口以实现方法签名:

public class PagedResponse<T> implements Page<MovieResponse>