我建立了一个简单的REST服务,我想从基于数据库的ID中获取数据密钥,但是当我在邮递员中未显示任何结果时,该如何解决呢?
这是我的控制器
//Get Key
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
gkrepo.getKeyByCompanyid(company_id);
return encKey;
}
这是我的资料库
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public void getKeyByCompanyid(Integer companyid);
}
这里的问题是事实,您忽略了repository
方法的返回值并返回null
。
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
gkrepo.findOneByCompanyId(company_id);
return encKey; //YOU RETURN NULL HERE
}
您需要做的是从KeyEntity
对象返回密钥。
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
return gkrepo.getKeyByCompanyid(company_id).getKey();
}
您还需要在中使用其他方法repository
。
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer> {
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public void findOneByCompanyId(Integer companyid);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句