java - 如何在spring boot java中编写一个函数来处理JPA存储库中的自定义查询?

     2023-04-14     116

关键词:

【中文标题】java - 如何在spring boot java中编写一个函数来处理JPA存储库中的自定义查询?【英文标题】:How to write a function to handle custom query in JPA repository in spring boot java? 【发布时间】:2019-09-03 12:14:14 【问题描述】:

基本上,我使用 Spring Boot 在服务器端托管一个数据库。我想从客户端编写自定义查询,该查询是用角度开发的,并从服务器端调用一个函数来给我所需的结果。

服务器端的函数如下所示:

List<rows> func(String customQuery)
  //fetch rows from table using this custom query and return those rows
  //which I can use in client side.

Below are examples of customQuery which I need to send from client side:
select * from table;
select * from table where id>10;
select * from table where id>20 and id<30;

到目前为止,我在互联网上搜索我找不到任何解决方案。请帮忙。

【问题讨论】:

这看起来像是一个巨大的安全漏洞。如何检索自定义查询?网络服务? 【参考方案1】:

直接从客户端发送 SQL 将导致完全的安全故障,因为恶意用户可以很容易地弄清楚它的工作原理并发送delete from table 来杀死您的整个应用程序。 更糟糕的是,他们甚至可以运行 create user ... 以完全访问您的整个数据库、获取敏感信息、安装恶意软件等。

相反,您需要使用诸如

之类的方法在您的应用程序中创建一个 REST 服务
GET /table
GET /table?minId=10
GET /table?minId=20&maxId=30

application/json 或类似的数据格式返回,仅返回您的 Angular 应用真正需要的信息。 然后 Angular 将负责选择性地使用您的数据更新显示。


编辑: 这是我找到的用于创建基于 Spring Boot 和 Angular 的基本 Web 应用程序的指南。对您来说可能是一个很好的起点: https://www.baeldung.com/spring-boot-angular-web

【讨论】:

【参考方案2】:

最好的方法是从客户端发送仅参数(在您的情况下为minIDmaxID),然后使用Spring JPA Specifications 在服务器端动态构建查询.

【讨论】:

Spring Boot应用程序失败-错误java包不存在

】SpringBoot应用程序失败-错误java包不存在【英文标题】:SpringBootapplicationfailure-Errorjavapackagedoesnotexist【发布时间】:2018-12-3115:12:36【问题描述】:我有一个引用外部模块来执行结果的SpringBoot项目。我已经在模块依赖下添加了外... 查看详情

java - 如何修复java Spring-boot中的“​​BeanCreationException”错误?

】java-如何修复javaSpring-boot中的“​​BeanCreationException”错误?【英文标题】:Howtofix"BeanCreationException"ErrorinjavaSpring-boot?【发布时间】:2019-04-0814:05:31【问题描述】:我在尝试在javaeclipse中运行Spring-boot应用程序时遇到问... 查看详情

如何在没有spring boot的情况下使用spring批处理的java配置?

】如何在没有springboot的情况下使用spring批处理的java配置?【英文标题】:HowcanIusejavaconfigurationofspringbatchwithoutspringboot?【发布时间】:2021-10-1106:05:57【问题描述】:我是SpringBatch的新手,我正在努力了解它。我实现了数据源和批... 查看详情

如何使用 gradle 6+ 和 java 11+ 在 Spring Boot 中配置 spock

】如何使用gradle6+和java11+在SpringBoot中配置spock【英文标题】:Howtoconfigurespockinspringbootwithgradle6+andjava11+【发布时间】:2020-11-2515:51:59【问题描述】:我想在我的spring-boot项目中使用spock(使用groovy,而不是java)。我有一些spock项目... 查看详情

如何在spring-boot可执行jar文件中配置java logging.properties

】如何在spring-boot可执行jar文件中配置javalogging.properties【英文标题】:Howtoconfigurejavalogging.propertiesinspring-bootexecutablejarfile【发布时间】:2016-07-0309:43:07【问题描述】:我将我的应用程序打包为spring-boot可执行jar文件。我在jar文件... 查看详情

如何在spring boot应用程序中配置Jackson而不覆盖纯java中的springs默认设置

】如何在springboot应用程序中配置Jackson而不覆盖纯java中的springs默认设置【英文标题】:HowtoconfigureJacksoninspringbootapplicationwithoutoverridingspringsdefaultsettinginpurejava【发布时间】:2018-07-0905:47:25【问题描述】:在我的SpringBoot应用程序... 查看详情

如何在所有请求标头中传递授权令牌 - Spring Boot java

】如何在所有请求标头中传递授权令牌-SpringBootjava【英文标题】:Howtopasstheauthorizationtokeninallrequestheader-Springbootjava【发布时间】:2020-12-0523:43:14【问题描述】:我的代码遇到问题。我需要在请求(K,V)标头中传递生成的令牌以... 查看详情

java - 如何在Java Spring Boot上的firebase存储中获取上传文件的downloadURL

】java-如何在JavaSpringBoot上的firebase存储中获取上传文件的downloadURL【英文标题】:HowtogetdownloadURLofuploadedfileinfirebasestorageonJavaSpringBoot【发布时间】:2022-01-1311:35:47【问题描述】:在JavaSpringboot中,我可以使用firebase-adminsdk获取签名... 查看详情

如何 JUnit 测试 Spring-Boot 的 Application.java

】如何JUnit测试Spring-Boot的Application.java【英文标题】:HowtoJUnitTestSpring-Boot\'sApplication.java【发布时间】:2017-06-0602:54:23【问题描述】:道歉,因为这似乎是一个无用的行为,但是我们有什么办法可以在Spring-Boot(1.3.8.RELEASE)的Applicati... 查看详情

如何在 Spring Boot 中注册自定义转换器?

】如何在SpringBoot中注册自定义转换器?【英文标题】:Howtoregistercustomconvertersinspringboot?【发布时间】:2016-03-1807:31:31【问题描述】:我使用spring-boot-starter-jdbc(v1.3.0)编写应用程序。我遇到的问题:BeanPropertyRowMapper的实例失败,因... 查看详情

在哪里以及如何免费部署 Java Spring Boot 应用程序? [关闭]

】在哪里以及如何免费部署JavaSpringBoot应用程序?[关闭]【英文标题】:WhereandhowtodeployaJavaSpringBootapplicationforfree?[closed]【发布时间】:2020-08-0721:28:32【问题描述】:我想知道在哪里以及如何部署使用SpringBoot构建的JavaWeb应用程序... 查看详情

如何使用 graphql-spring-boot 向 GraphQL Java 添加检测?

】如何使用graphql-spring-boot向GraphQLJava添加检测?【英文标题】:HowtoaddinstrumentationtoGraphQLJavawithgraphql-spring-boot?【发布时间】:2020-05-1604:49:40【问题描述】:有人知道在使用graphql-spring-boot(https://github.com/graphql-java-kickstart/graphql-spr 查看详情

如何在 Java Spring Boot MVC 中使用 Ajax 删除多个项目

】如何在JavaSpringBootMVC中使用Ajax删除多个项目【英文标题】:HowtodeletemultipleitemsusingAjaxinJavaSpringBootMVC【发布时间】:2020-05-0518:07:13【问题描述】:我正在尝试使用复选框和删除按钮来删除表格中的多个项目。我尝试在不同的论... 查看详情

如何在 Java Spring Boot 中为外部配置设置 PropertySourcesPlaceholderConfigurer Auto SetLocations

】如何在JavaSpringBoot中为外部配置设置PropertySourcesPlaceholderConfigurerAutoSetLocations【英文标题】:HowtosettingPropertySourcesPlaceholderConfigurerAutoSetLocationsforExternalConfigurationinJavaSpringBoot【发布时间】:2020-08-1921:25:38【问题描述】 查看详情

如何在 Spring Boot CrudRepository 中搜索数组

】如何在SpringBootCrudRepository中搜索数组【英文标题】:HowtosearchthrougharrayinSpringBootCrudRepository【发布时间】:2018-07-0314:55:40【问题描述】:说,我有以下实体类:Person.java@EntitypublicclassPerson@IdprivateStringname;privateString[]cars;//Constructor 查看详情

如何在spring boot中重写java.sql.Array Descriptor和java.sql.ARRAY?

】如何在springboot中重写java.sql.ArrayDescriptor和java.sql.ARRAY?【英文标题】:Howtorewritejava.sql.ArrayDescriptorandjava.sql.ARRAYinspringboot?【发布时间】:2019-12-0705:14:22【问题描述】:之前我用这段代码调用了一个Oracle存储过程:ArrayDescriptorarr... 查看详情

如何在 Spring Boot + GraphQL Java 工具的上下文中执行对 GraphQL 的 Java 调用?

】如何在SpringBoot+GraphQLJava工具的上下文中执行对GraphQL的Java调用?【英文标题】:HowtoexecuteJavacallstoGraphQLinaSpringBoot+GraphQLJavaTools\'context?【发布时间】:2020-01-3023:46:32【问题描述】:在SpringBoot应用程序中,我们已经拥有一个功能... 查看详情

如何在 Spring Boot 应用程序中将 java lang long 转换为实体类

】如何在SpringBoot应用程序中将javalanglong转换为实体类【英文标题】:HowtocastjavalanglongtoentityclassinSpringBootapplication【发布时间】:2021-12-1407:00:55【问题描述】:我创建了一个应用程序,当我去更新我的数据库中的一列时,我得到... 查看详情