繁体   English   中英

如何从基于数据库的 Id SpringBoot Java 获取数据

[英]How to get Data from database Based Id SpringBoot Java

我构建了简单的 REST 服务,我想从基于数据库的 id 中获取数据密钥,但是,当我运行 postman 中没有显示结果时,我该如何解决? 请帮帮我....

这是我的存储库

public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
Optional<KeyEntity> findById(Integer id);

}

这是我的 Controller

@GetMapping(path= "/getById/{company_id}")

String getById(@RequestBody KeyEntity keyEntity, @PathVariable int company_id){
String encKey= null;

    KeyEntity key = new KeyEntity();

    encKey= key.getKeyencrypted();

    gkrepo.findById(company_id);

    return encKey;

}

这是我的实体

@Entity
@Table(name= "tb_key")
public class KeyEntity {

@Id
private Integer companyid;
private Date creationdate;
private String keyencrypted;

public Integer getCompanyid() {
    return companyid;
}
public void setCompanyid(Integer companyid) {
    this.companyid = companyid;
}
public Date getCreationdate() {
    return creationdate;
}
public void setCreationdate(Date creationdate) {
    this.creationdate = creationdate;
}
public String getKeyencrypted() {
    return keyencrypted;
}
public void setKeyencrypted(String keyencrypted) {
    this.keyencrypted = keyencrypted;
}
@Override
public String toString() {
    return "KeyEntity [companyid=" + companyid + ", creationdate=" + creationdate + ", keyencrypted=" + keyencrypted
            + "]";
}



}

赋值gkrepo.findById(company_id); 到一个变量,然后从该变量中获取加密密钥。 然后从您的 controller 返回。 目前,您正在自己创建一个新的 KeyEntity object,而不是使用存储库结果。

试试下面的代码。 它可能会有所帮助。

@GetMapping(path= "/getById/{company_id}")
String getById(@RequestBody KeyEntity keyEntity, @PathVariable int company_id){
String encKey= null;
KeyEntity key = gkrepo.findById(company_id).get();
encKey= key.getKeyencrypted();
return encKey;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM