[英]How to implement a function to create post request in springboot?
In the Springboot project I'm currently doing,I use Postman client to send post request which includes a JSON body.Following image is relevant to that.在我目前正在做的 Springboot 项目中,我使用 Postman 客户端发送包含 JSON 主体的发布请求。下图与此相关。
post request= http://localhost:8080/api/v1/orders发布请求= http://localhost:8080/api/v1/orders
Json Body= { Json 身体= {
"fuelType": "Petrol 95", "qty": 10000 “燃料类型”:“汽油 95”,“数量”:10000
} }
Can someone help me how to do this post request(which is in the image) using a function without using postman client.有人可以帮助我如何在不使用 postman 客户端的情况下使用 function 来执行此发布请求(在图像中)。
You can use cURL (https://curl.se/) :您可以使用 cURL (https://curl.se/) :
curl -X POST
-H "Content-Type: application/json"
-d '{"fuelType": "Petrol 95","qty": 10000}'
but if you want to make the call from java, you can use any http client from your choice, here is an example using resttemplate:但如果您想从 java 拨打电话,您可以使用您选择的任何 http 客户端,这里是使用 resttemplate 的示例:
@Data
@AllArgsConstructor
class requestDTO{
private String fuelType;
private Integer qty;
}
import org.springframework.web.client.RestTemplate;
...
RestTemplate restTemplate = new RestTemplate();
HttpEntity<requestDTO> request = new HttpEntity<>(new requestDTO("Petrol 95",10000));
requestDTO output = restTemplate.postForObject("http://localhost:8080/api/v1/orders", request, requestDTO.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.