gpt4 book ai didi

java - 如何将数据从servlet发送到rest api

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:56 26 4
gpt4 key购买 nike

我想将数据从我的 servlet 发送到 rest api。它是如何完成的:

protected void doPost(
HttpServletRequest request
, HttpServletResponse response
) throws ServletException, IOException {

String Id= "MyId";

response.setContentType("application/json");
response.getWriter().write(Id);
getServletContext()
.getRequestDispatcher("<PathofAPI>")
.forward(request, response);
}

一旦发送了数据,如何在我的 rest api 中检索它

最佳答案

或者,您必须使用 getter 和 setter 为您的 Id 参数创建 POJO 类:

String createRequestUrl="YOUR_LINK WHERE_YOU GET answer FROM";
RestTemplate template=new RestTemplate();
your_POJO_object.setYour_Pojo_Object(Id);
ObjectMapper objectMapper = new ObjectMapper();
MultiValueMap<String, String> orderRequestHeaders=new
LinkedMultiValueMap<String,String>();
orderRequestHeaders.add("Content-Type", "application/json");
orderRequestHeaders.add("Accept", "application/json");
String orderCreateRequest=objectMapper.writeValueAsString(YOUR POJO object.class);
HttpEntity<String> orderRequest=new HttpEntity<String>(orderCreateRequest, orderRequestHeaders);
String response=template.postForObject(createRequestUrl, orderRequest, String.class);

关于java - 如何将数据从servlet发送到rest api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55513456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com