简体   繁体   English

如何在springboot中实现一个function来创建post请求?

[英]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 主体的发布请求。下图与此相关。

image图片

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.

相关问题 无法反序列化从 Angular http 对我的 Springboot POST 映射的 function 的发布请求发送的 POJO - Can't deserialize my POJO sent from an Angular http post request on my Springboot POST mapped function Django:for循环和POST请求创建功能 - Django: for loop and POST request create function 如何使用有效负载请求在 python 中创建发布请求? - How to create a post request in python with payload request? 在 SpringBoot 的 JSON 内发布一个没有 JSON 的请求 - POST a request with NO JSON inside a JSON in SpringBoot 如何在 firefox 扩展中创建 JSON 发布请求? - How to create a JSON post request in firefox extension? 如何快速创建JSON主体用于发布请求 - How to create JSON Body for Post request in swift 如何在 SwiftUI 中创建一个 json &#39;POST&#39; 请求? - How to create a json 'POST' request in SwiftUI? 请求规范:如何通过POST请求创建新用户? - Request spec: How to create a new user via POST request? 使用邮递员发布请求后,springboot 服务器返回错误 400 - springboot server returns error 400 after post request using postman 带有 JSON 主体的 POST 请求不会传递到 springboot 微服务架构中的端点 - POST request with JSON body do not deliver to the endpoint in springboot microservice architecture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM