我建立了一个简单的REST服务,我想从基于数据库的id中获取数据,只是数据密钥,但是当我在邮递员中未显示任何结果时,该如何解决呢?
请帮忙...

这是我的资料库

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

}


这是我的控制器

@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
            + "]";
}



}

最佳答案

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

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


}

关于java - 如何从基于数据库的Id Spring Boot Java获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58869534/

10-11 22:37
查看更多