Reputation: 71
I want to send data from my servlet to a rest api . Is it how it's done :
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);
}
And once the data is send how to retreive it in my rest api
Upvotes: 3
Views: 660
Reputation:
Alternatively you must create POJO class for you Id parameter with getters and setters:
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);
Upvotes: 2
Reputation: 149
What you want to acheive is a bit unclear to me.
By writing "MyId" to the response before letting "PathofServet" process the request, you're breaking the json format in the response.
Have you an exemple of what the servlet "PathofServlet", expects as request and send as response ? And what you would like your servlet to response ?
Upvotes: 0