【问题标题】:Returning different Resource type Lists from one method从一种方法返回不同的资源类型列表
【发布时间】:2019-04-22 19:47:00
【问题描述】:

我有这个方法用于嵌入参数的 GET 请求。主要思想是,如果链接包含 ?embedded=true 则返回实体,如果不为 true 则返回 DTO 对象:

@GetMapping("/todos") 
    public List<Resource<?>> getAllToDoNote(@RequestParam(value = "embedded",required =false)String embedded){
        List<Resource<ToDoNote>> noteResources = new ArrayList<Resource<ToDoNote>>();
        List<ToDoNote> allNotes = toDoNoteService.getAllToDoNote();

        if(embedded!=null && embedded.equals("true")) {
            for(int i=0; i< allNotes.size();i++) {
            Resource<ToDoNote> noteResource = new Resource<>(allNotes.get(i));
            //Link linkTo = linkTo(methodOn(this.getClass()).getNotesUsers(allNotes.get(i).getId())).withRel("users");

            //noteResource.add(linkTo);
            noteResources.add(noteResource);
            }

            //return toDoNoteService.getAllToDoNote();
            return noteResources;
        }

        else {
            //System.out.println("embedded " +embedded);
            List<Resource<ToDoNoteDTO>> dtoResources = new ArrayList<Resource<ToDoNoteDTO>>();
            notes = toDoNoteService.getAllToDoNote();
            List<ToDoNoteDTO> noteDtos = new ArrayList<ToDoNoteDTO>();
            for(int i=0; i<notes.size(); i++) {
                //System.out.println("size" +notes.get(i).getName());
                noteDtos.add(convertToDto(notes.get(i)));
            }

            for(int i=0; i< noteDtos.size();i++) {
                Resource<ToDoNoteDTO> dtoResource = new Resource<>(noteDtos.get(i));
                Link linkTo = linkTo(methodOn(this.getClass()).getNotesUsers(allNotes.get(i).getId())).withRel("users");

                dtoResource.add(linkTo);
                dtoResources.add(dtoResource);
                }

            return dtoResources;
            //return null;
        }

    }


@Validated

public class ToDoNote {
    private Integer id;
    @NotNull(message = "Name may not be null")
    private String name;
    @JsonFormat(pattern="yyyy-MM-dd")
    private Date dateToComplete;
    private String description;
    private Integer priority;
    private Boolean completed;
    private ArrayList<User> users; 
....

}

public class ToDoNoteDTO {
    private Integer id;
    @NotNull(message = "Name may not be null")
    private String name;
    @JsonFormat(pattern="yyyy-MM-dd")
    private Date dateToComplete;
    private String description;
    private Integer priority;
    private Boolean completed;
....
}

但是,我在返回行中遇到错误: 类型不匹配:无法从 List> 转换为 List >。如果我在任何地方放置问号,它可以让我运行程序,但是它会返回其他字段为空的链接。 还有其他方法可以返回两种不同的类型吗?因为我需要返回一个完整的实体或一个不包含所有字段的 DTO。

编辑

我已经能够使用 Resources> 而不是 List 让它工作,但是现在有 _embedded 属性,经过一番搜索后似乎没有办法将其删除。所以我只想了解如何处理这种情况,根据给定的嵌入参数来响应不同的实体。谢谢。

EDIT2 添加实体和 DTO 类。

【问题讨论】:

  • 既然有DTO,那Entity为什么要暴露给客户端呢?
  • 如果他们希望使用 ?embedded 参数查看整个实体,那么对于我的程序来说很好。这是所需的功能。
  • 为什么不总是提供整个对象?或者,如果这会使对象太大,则通过提供links within the response 使其更加 RESTful。
  • 我确信有更好的方法,但这是我需要做的。此外,当我提供 DTO 时,我会提供链接,如果它是 embedded=true,那么我会提供对象本身而不是链接。
  • @SvajunasKavaliauskas 更多信息为什么您的链接无法正确呈现:github.com/spring-projects/spring-hateoas/issues/… 您似乎对使用 HAL 不感兴趣,您确定 Spring HATEOAS 适合您吗?

标签: java spring spring-boot spring-hateoas


【解决方案1】:

如果您想根据用户的需要返回不同的字段,我建议您使用 spring 数据休息预测:请参阅 https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多