![](/img/trans.png)
[英]How to capture metrics Separately for GET,POST and PUT method for given REST Endpoint using spring boot actuators
[英]How to pass multiple ids in API Get method Endpoint using Spring Boot
我正在嘗試在 GET 端點中傳遞多個 ID。 例如:如果我的數據庫包含以 ID 作為主鍵的 10 名員工的詳細信息,並且假設我想在特定點的 Endpoint 中傳遞多個 id。 這可能嗎。 認為:
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(","));
..........
}
現在您在ids字段中有一個 id 列表。
你的要求應該是這樣的
localhost:9363/products/1,2,...
不要添加任何類型的括號,然后在您的控制器類中,您可以創建一個如下所示的 API
@GetMapping("/products/{Ids}")
public List<Product> getProducts(@PathVariable List<Long> Ids)
{
return this.service.getProducts(Ids); // calling the service class as per
the architecture
}
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.