【问题标题】:How to get Data By Id From Database Spring Boot Java REST API如何通过 ID 从数据库 Spring Boot Java REST API 获取数据
【发布时间】:2019-11-14 07:26:47
【问题描述】:

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

这是我的控制器

    //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 注释添加到您的存储库?
  • 您返回的 encKey 始终为空。
  • 我不确定您如何期望从 void 方法返回某些内容。 public void getKeyByCompanyid(Integer companyid);

标签: java postgresql spring-boot web-services


【解决方案1】:

这里的问题是,您忽略了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);
}

【讨论】:

  • 如果我想使用 return 我该如何创建?我只想获取基于数据密钥的 id,并返回 encKey? @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);返回加密密钥; }
【解决方案2】:

您应该如下更改存储库中的方法。试试这个。

 public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
 {

@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();

public KeyEntity findByCompanyId(Integer companyid);
 }

【讨论】:

  • 为什么不直接使用继承的findById() 方法?
  • 因为它使用 KeyEntity id 而不是 CompanyId
【解决方案3】:

你的控制器应该是:

@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;

KeyEntity keyEntity = gkrepo.getKeyByCompanyid(company_id);

return keyEntity.getKey;

}

您的存储库应该是这样的:

public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
 {

@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();

public KeyEntity findByCompanyId(Integer companyid);
 }

【讨论】:

【解决方案4】:

您可以尝试如下更改您的方法

public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
  {

    @Query(value= "SELECT * FROM tb_key", nativeQuery = true)
    List<KeyEntity> getAll();

    public KeyEntity findByCompanyId(Integer companyid);
  }

如果您使用此代码,则必须按如下方式更改代码

gkrepo.findByCompanyId

而不是

gkrepo.getKeyByCompanyid(company_id);

public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
 {

    @Query(value= "SELECT * FROM tb_key", nativeQuery = true)
    List<KeyEntity> getAll();

    @Query(Select k from KeyEntity k where companyid = :companyid)    
    public KeyEntity getKeyByCompanyid(@Param("companyid") Integer companyid);
 }

【讨论】:

  • 为什么不直接使用继承的 findById() 方法?
  • 我不知道如何@Ulfa Hartina 在实体中写入列名,如果该列属性名是 id ,他可以使用 findById() 方法。
【解决方案5】:

您正在使用 Spring Data JPA。您的存储库接口从扩展的JpaRepository 接口继承了各种方法。这就是它的全部意义所在。

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html

那么就没有读写查询方法了:

@RestController
public class myController{

    @RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
    public KeyEntity getKey(@PathVariable("company_id") int companyId) {

        return gkrepo.findById(companyId); //inherited method
    }

}

此外,如果您启用 Spring Data JPA 的 Web 扩展,则根本不需要调用存储库,因为实体将自动从路径变量中解析:

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web

DomainClassConverter 允许您在 Spring MVC 中使用域类型 控制器方法签名直接,因此您无需手动 通过存储库查找实例

@RestController
public class myController{

    @RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
    KeyEntity getKey(@PathVariable KeyEntity keyEntity) {

        return keyEntity; 
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2020-12-05
    • 2015-05-16
    • 1970-01-01
    • 2015-11-17
    • 2017-07-15
    相关资源
    最近更新 更多