当前位置: 首页 > 知识库问答 >
问题:

如何使用SpringDataREST将自定义DTO公开到crud存储库?

单于俊智
2023-03-14

我不想公开我的模型类(jpa实体),而是用不同的数据传输对象(DTO)公开它们属性的不同子集。这个想法是不切实际的

例子:

实体:

@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;
    }

}

D到积垢积存物:

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();
    }

}

一般:

@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中,我可以自动连接GroupDto和GroupDto,所以两者都由Spring的上下文管理。如果我不使用@RepositoryRestResources(导出=false)注释GroupDo,JpaRepository将公开为REST服务,因此我认为Spring数据REST配置良好。

我怎么能让它暴露我的定制积垢?

共有1个答案

严正诚
2023-03-14

有一个JIRA问题需要澄清如何做到这一点。

目前,SDR团队表示,“我们通常建议只使用杰克逊混音器来钩住自定义序列化器、自定义输出等。例如,请参阅Spring RESTBuck。"

 类似资料:
  • 我有一个实体类称为和一个仓库声明如下: 自定义存储库的定义如下: 和 下面是另一个堆栈溢出问题中描述的内容:如何向Spring数据JPA添加自定义方法 现在,IntelliJ给了我一个警告: 我尝试将这些注释添加到中,但没有一个成功: 但都不管用。显然有些是错的,但我在试验。正确的注释是什么。 使用,我得到了以下错误:

  • 我想从maven转到gradle。 在pom中。xml文件我有一个自定义存储库: 它是一个简单的超文本传输协议Web服务器,带有. jar文件。 如何将此自定义回购添加到生成中。格拉德尔? 我尝试了此代码,但不起作用: 我的定制repo不是maven repo,但我在Gradle文档中没有找到可以指定URL的其他示例。

  • 如果你需要提供自定义文件存储 – 一个普遍的例子是在某个远程系统上储存文件 – 你可以通过定义一个自定义的储存类来实现。你需要遵循以下步骤: 1. 你的自定义储存类必须是django.core.files.storage.Storage的子类: from django.core.files.storage import Storage class MyStorage(Storage):

  • 在我的项目中有几个实体具有相同的属性(对于示例'name'),所以,有可能创建一个存储库,其中使用自定义的select(实体)?因此,我从JpaRepository扩展了我的存储库,我扩展了MyCustomJpaRepository,MyCustomJpaRepository也扩展了JpaRepository,使其能够从JpaRepository授予基本功能? TKS

  • 我是Gradle/Groovy的新手,所以我可能遗漏了一些显而易见的东西。你能帮忙吗? 我们使用Ivy进行依赖管理。我正在试用Gradle,希望与我们现有的常春藤基础设施集成。通常情况下,这应该是可能的,但我们的常春藤的布局有点特别,而且...我不能让它工作。 这是因为我们的常春藤在布局时考虑了组织的url,例如: 我现在试着把这句话翻译成Gradle: 这当然是失败的,因为“[organizat

  • 项目配置为使用多个MongoTemplate Mongo Ref传递为 问题:我需要访问MongoTemplate,它是类似的标准存储库。 例如,如果正在将接口扩展为 MyRepoCustomImpl 问题:相反,难道没有任何方法可以让要使用的MongoTemplate根据它扩展到的Repo自动注入或解析吗?