使用 Spring Rest 将 ID 转换为类类型

我正在尝试使用 Spring Boot 开发通用的 CRUD,我的通用实体基类使用serializable


@MappedSuperclass

public abstract class Entidade<T extends Serializable> implements Serializable {


    private static final long serialVersionUID = 1L;


    public abstract T getId();

    public abstract T setId(T id);


}


@Entity

@Table(name = "cachorro")

@Getter

@Setter

@NoArgsConstructor

public class Cachorro extends Entidade<Long> {


    @Id

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cachorro_seq") 

    private Long id;


    @Column(length = 100, nullable = false)

    private String nome;


    @Override

    public Long getId() {

        return this.id;

    }


    @Override

    public Long setId(Long id) {

        return this.id;

    }


}

因此,当我调用休息方法时,它会抛出一个异常,告诉您使用存储库方法时的转换。


@NoRepositoryBean

public interface AbstratoRepository<E extends Entidade<?>> extends JpaRepository<E, Serializable> {

}


@Repository

public interface CachorroRepository extends AbstratoRepository<Cachorro> {

}

套餐服务


public interface IAbstratoService<E> {


    public List<E> carregarTodos();

    public Optional<E> carregarPeloCodigo(Serializable id);

    public void inserir(E entidade);

    public void atualizar(E entidade);

    public void deletar(Serializable id);


}


public class AbstratoServiceImpl<T extends Serializable, E extends Entidade<?>, R extends AbstratoRepository<E>>

        implements IAbstratoService<E> {


    @Autowired

    private R repositorio;


    @Override

    public List<E> carregarTodos() {

        return this.repositorio.findAll();

    }


    @Override

    public Optional<E> carregarPeloCodigo(Serializable id) {

        return this.repositorio.findById(id);

    }


    @Override

    public void inserir(E entidade) {

        this.repositorio.save(entidade);

    }


    @Override

    public void atualizar(E entidade) {

        this.repositorio.save(entidade);

    }


    @Override

    public void deletar(Serializable id) {

        this.repositorio.deleteById(id);

    }

}


@Service

public class CachorroService extends AbstratoServiceImpl<Long, Cachorro, CachorroRepository> {

}


慕尼黑5688855
浏览 86回答 1
1回答

慕田峪7331174

我非常确定这里的错误如下 => AbstratoServiceImpl 和 AbstratoResource 应该使用 T 并且不可序列化 id 字段。以下是一个很好的起点。public abstract class AbstratoResource<ID, E extends Entidade<ID>, S extends IAbstratoService<E>> {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; protected S service;&nbsp; &nbsp; @GetMapping("/carregarTodos")&nbsp; &nbsp; public List<E> carregarTodos() {&nbsp; &nbsp; &nbsp; &nbsp; return this.service.carregarTodos();&nbsp; &nbsp; }&nbsp; &nbsp; @GetMapping("/carregarPeloCodigo/{id}")&nbsp; &nbsp; public Optional<E> carregarPeloCodigo(@PathVariable("id") ID id) {&nbsp; &nbsp; &nbsp; &nbsp; return this.service.carregarPeloCodigo(id);&nbsp; &nbsp; }&nbsp; &nbsp; @PostMapping("/inserir")&nbsp; &nbsp; public void inserir(E entidade) {&nbsp; &nbsp; &nbsp; &nbsp; this.service.inserir(entidade);&nbsp; &nbsp; }&nbsp; &nbsp; @PutMapping("/atualizar")&nbsp; &nbsp; public void atualizar(E entidade) {&nbsp; &nbsp; &nbsp; &nbsp; this.service.atualizar(entidade);&nbsp; &nbsp; }&nbsp; &nbsp; @DeleteMapping("/deletar")&nbsp; &nbsp; public void deletar(ID id) {&nbsp; &nbsp; &nbsp; &nbsp; this.service.deletar(id);&nbsp; &nbsp; }}public interface IAbstratoService<E> {&nbsp; &nbsp; public List<E> carregarTodos();&nbsp; &nbsp; public Optional<E> carregarPeloCodigo(ID id);&nbsp; &nbsp; public void inserir(E entidade);&nbsp; &nbsp; public void atualizar(E entidade);&nbsp; &nbsp; public void deletar(ID id);}public class AbstratoServiceImpl<ID, T extends Serializable, E extends Entidade<ID>, R extends AbstratoRepository<E>>&nbsp; &nbsp; &nbsp; &nbsp; implements IAbstratoService<E> {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private R repositorio;&nbsp; &nbsp; @Override&nbsp; &nbsp; public List<E> carregarTodos() {&nbsp; &nbsp; &nbsp; &nbsp; return this.repositorio.findAll();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Optional<E> carregarPeloCodigo(ID id) {&nbsp; &nbsp; &nbsp; &nbsp; return this.repositorio.findById(id);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void inserir(E entidade) {&nbsp; &nbsp; &nbsp; &nbsp; this.repositorio.save(entidade);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void atualizar(E entidade) {&nbsp; &nbsp; &nbsp; &nbsp; this.repositorio.save(entidade);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void deletar(ID id) {&nbsp; &nbsp; &nbsp; &nbsp; this.repositorio.deleteById(id);&nbsp; &nbsp; }}@Servicepublic class CachorroService extends AbstratoServiceImpl<Long, Cachorro, CachorroRepository> {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java