Java 如何使用Spring Boot在API Get方法端点中传递多个ID
我试图在GET端点中传递多个ID。 例如:如果我的数据库包含10名员工的详细信息,ID作为主键,并且假设我希望在端点的特定点传递多个ID。这可能吗。 假设:Java 如何使用Spring Boot在API Get方法端点中传递多个ID,java,spring-boot,spring-jdbc,Java,Spring Boot,Spring Jdbc,我试图在GET端点中传递多个ID。 例如:如果我的数据库包含10名员工的详细信息,ID作为主键,并且假设我希望在端点的特定点传递多个ID。这可能吗。 假设: http://localhost:8080/api/v/listempoloyee/{1,2,3,4} {1,2,3,4}是我们要从数据库中获取的雇员ID的列表 使用Spring Boot和JDBC是否可以做到这一点。这应该可以做到 @GetMapping("/{employeeIds}") public void tri
http://localhost:8080/api/v/listempoloyee/{1,2,3,4}
{1,2,3,4}是我们要从数据库中获取的雇员ID的列表
使用Spring Boot和JDBC是否可以做到这一点。这应该可以做到
@GetMapping("/{employeeIds}")
public void trigger(@PathVariable String employeeIds) {
List<String> ids = Arrays.asList(employeeIds.split(","));
..........
}
@GetMapping(“/{employeeIds}”)
public void触发器(@PathVariable字符串employeeIds){
List id=Arrays.asList(employeeid.split(“,”);
..........
}
现在,您在ids字段中有一个ID列表。您可以向控制器发送post请求。请按照以下步骤操作-:
you can send a post request to your controller.please follow these steps-:
1. **I need to create a html file because of ajax calling.**
<!DOCTYPE html>
<html>
<head>
<title>Ajax Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/js/ajaxCall.js"></script>
</head>
<body>
<button type="button" class="btn btn-sm btn-update btn-primary" id="send">
<span class="glyphicon glyphicon-ok"></span>Send Parameter
</button>
</body>
</html>
2. **create a js file and create a function for ajax calling.**
$(document).ready(function() {
$("#send").on("click",function(){
var paramIds = ["1", "2", "3", "4","5"];
var parameterData = JSON.stringify({
'paramList' :paramIds
});
$.ajax({
contentType: 'application/json',
method:'post',
data: parameterData,
url: "http://localhost:8080/get",
dataType: 'json',
}).done(function(data, textStatus, xhr, options) {
}).fail(function(xhr, textStatus, errorThrown) {
}).always(function(xhr, textStatus) {
});
})
});
please focus on **JSON.stringify**.JSON.stringify convert javascript object to a string following the JSON notation.
3. **create a form to accept paramter as a post request.**
package com.example.demo;
import java.util.List;
import java.util.ArrayList;
import lombok.Data;
@Data
public class ParameterForm {
List<String> paramList=new ArrayList<>();
}
4. **create a controller class for accepting the post request.**
package com.example.demo;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping(value="/home")
public String checkParam()
{
return "test";
}
@RequestMapping(value="/get",method = RequestMethod.POST)
@ResponseBody
public String getId(@RequestBody ParameterForm parameterForm)
{
List<String> paramList=parameterForm.getParamList();
for(String param:paramList)
{
System.out.println(param);
}
return "success";
}
}
please focus on getId controller.
you will find **@RequestBody ParameterForm parameterForm**.This form will accept the parameter which i send in ajax call.
******************output**********************
1
2
3
4
5
1. **由于ajax调用,我需要创建一个html文件**
Ajax测试
发送参数
2. **创建一个js文件,并为ajax调用创建一个函数**
$(文档).ready(函数(){
$(“#发送”)。在(“单击”,函数(){
var paramIds=[“1”、“2”、“3”、“4”、“5”];
var parameterData=JSON.stringify({
“paramList”:paramIds
});
$.ajax({
contentType:'应用程序/json',
方法:'post',
数据:参数数据,
url:“http://localhost:8080/get",
数据类型:“json”,
}).done(函数(数据、文本状态、xhr、选项){
}).fail(函数(xhr、textStatus、errorshown){
}).always(函数(xhr、textStatus){
});
})
});
请关注**JSON.stringify**.JSON.stringify将javascript对象转换为JSON符号后面的字符串。
3. **创建一个表单以接受参数作为post请求**
包com.example.demo;
导入java.util.List;
导入java.util.ArrayList;
导入龙目数据;
@资料
公共类参数形式{
List paramList=new ArrayList();
}
4. **创建用于接受post请求的控制器类**
包com.example.demo;
导入java.util.List;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.ResponseBody;
@控制器
公共类测试控制器{
@请求映射(value=“/home”)
公共字符串checkParam()
{
返回“测试”;
}
@RequestMapping(value=“/get”,method=RequestMethod.POST)
@应答器
公共字符串getId(@RequestBody ParameterForm ParameterForm)
{
List paramList=parameterForm.getParamList();
for(字符串参数:参数列表)
{
系统输出打印项次(参数);
}
返回“成功”;
}
}
请关注getId控制器。
您将发现**@RequestBody ParameterForm ParameterForm**。此表单将接受我在ajax调用中发送的参数。
******************输出**********************
1.
2.
3.
4.
5.
可能会有帮助,谢谢你的建议,但是我如何在SQL查询中传递ID呢。我的意思是,对于单个员工的详细信息,我只是简单地触发“jdbcTemplate.query(“SELECT*FROM device_registration WHERE Id=?”,new DeviceRowMapper(),Id)”;“但是对于传递多个Id,我也必须更改查询。我还需要如何在端点中传递Id。我的意思是“/employeeList/{1,2,3}”。