簡體   English   中英

如何使用Spring數據REST公開自定義DTO crud存儲庫?

[英]How to expose custom DTO crud repository with Spring data REST?

我不想暴露我的模型類(jpa實體),而是將它們的屬性的不同子集暴露給不同的數據傳輸對象(DTO)。 想法是DTO CrudRepository <-> JpaRepository <-> entities ,我想通過Spring Data REST公開DTO CrudRepository

例:

實體:

@Entity
@Table(name = "groups")
public class Group {

    private Long id;
    private String name;
    private Set<User> users;
    // other attributes

    @Id
    @GeneratedValue
    @Column(name = "group_id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(mappedBy = "group")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    // other getters and setters

}

JpaRepository:

@RepositoryRestResource(exported = false)
public interface GroupDao extends JpaRepository<Group, Long> {
}

DTO:

public class GroupWithoutRelationsDto {

    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NotBlank
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

DTO CrudRepository:

public interface GroupDtoDao extends CrudRepository<GroupWithoutRelationsDto, Long> {
}

執行:

@Repository
public class GroupDtoDaoImpl extends GenericDtoDao<GroupWithoutRelationsDto, Group, Long> implements GroupDtoDao {

    @Autowired
    private GroupDao groupDao;

    @Override
    protected CrudRepository<Group, Long> getModelDao() {
        return groupDao;
    }

    @Override
    protected <S extends GroupWithoutRelationsDto> Long getDtoId(S dto) {
        return dto.getId();
    }

    @Override
    protected Long getModelId(Group model) {
        return model.getId();
    }

    @Override
    protected <S extends GroupWithoutRelationsDto> S modelToDto(Group model, S dto) {
        dto.setId(model.getId());
        dto.setName(model.getName());
        return dto;
    }

    @Override
    protected <S extends GroupWithoutRelationsDto> Group dtoToModel(S dto, Group model) {
        model.setId(dto.getId());
        model.setName(dto.getName());
        return model;
    }

    @Override
    protected Group newModel() {
        return new Group();
    }

    @Override
    protected GroupWithoutRelationsDto newDto() {
        return new GroupWithoutRelationsDto();
    }

}

GenericDtoDao:

@NoRepositoryBean
public abstract class GenericDtoDao<D, M, ID extends Serializable> implements CrudRepository<D, ID> {

    protected abstract CrudRepository<M, ID> getModelDao();

    protected abstract <S extends D> ID getDtoId(S dto);

    protected abstract ID getModelId(M model);

    protected abstract <S extends D> S modelToDto(M model, S dto);

    protected abstract <S extends D> M dtoToModel(S dto, M model);

    protected abstract M newModel();

    protected abstract D newDto();

    @Override
    public D findOne(ID id) {
        return modelToDto(getModelDao().findOne(id), newDto());
    }

    @Override
    public <S extends D> S save(S entity) {
        Assert.notNull(entity, "The entity must not be null!");
        if (getDtoId(entity) == null) {
            return create(entity);
        }
        return update(entity);
    }

    protected <S extends D> S create(S entity) {
        Assert.notNull(entity, "The entity must not be null!");
        if (getDtoId(entity) != null) {
            Assert.isTrue(!exists(getDtoId(entity)), "The entity ID must be null or not exist!");
        }
        return modelToDto(getModelDao().save(dtoToModel(entity, newModel())), entity);
    }

    protected <S extends D> S update(S entity) {
        Assert.notNull(entity, "The entity must not be null!");
        M model = getModelDao().findOne(getDtoId(entity));
        Assert.notNull(model, "The entity must exist!");
        return modelToDto(getModelDao().save(dtoToModel(entity, model)), entity);
    }

    // other CrudRepository methods

}

在這個例子中,我想用Spring數據REST公開GroupDtoDao。

在其他bean中,我可以自動裝配GroupDao和GroupDtoDao,因此兩者都由Spring的上下文管理。 如果我不使用GroupDao @RepositoryRestResource(exported = false)注釋GroupDao ,則JpaRepository將作為REST服務公開,因此我認為Spring數據REST配置得很好。

如何告訴它公開我的自定義CrudRepository?

一個JIRA問題可以澄清如何執行此操作。

目前,SDR團隊表示“我們通常建議只使用Jackson mixins來連接自定義序列化程序,自定義輸出等。請參閱Spring RESTBucks作為示例。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM