简体   繁体   English

如何通过API Spring Boot获取特定字段

[英]How to get the specific fields by API Spring Boot

I need to REST the request by the GET to transfer to the parameters of the field that I want to get from the database sql (from hibernate) 我需要通过GET还原请求,以将其传输到我想从数据库sql(从休眠)获取的字段的参数

@GetMapping("/product")
public Object getProducts(@RequestParam(name = "fields") String fields) {
   // how?
}



@Repository
public interface ProductRuRepository extends JpaRepository<Product, Long> {
@Query("SELECT ?1 FROM Product p")
List<Product> findByFields(String fields);
}

I'm waiting to get the fields that I passed to the parameter. 我正在等待获取传递给参数的字段。 But the project does not compile 但是项目没有编译

Please change your code to this structure: 请将您的代码更改为以下结构:

Associate product repository to your rest controller. 将产品存储库与您的剩余控制器关联。

   @RestController
   class ProductController {

    ProductRuRepository productRepository;

    @GetMapping("/product")
    public List<Product> getProducts(@RequestParam(name = "fields") String fields) 
    {
    List<Product> productList = productRepository.findByFields(fields);
    return productList;  
    }

    }

Your repository will trigger runtime exception because of syntax issue in JPA Query. 由于JPA Query中的语法问题,您的存储库将触发运行时异常。

Change the query as mentioned below. 如下所述更改查询。

@Query(value = "SELECT :fields FROM Product as p")
List<Product> findByFields(String fields);

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

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