I would like to pass String type strings in a different way than query parameters for server-client communication in Rest API.The sources we tried are as follows:
Client Side
HttpHeaders heads = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PLAIN_TEXT);
String sendparam = "test";
HttpEntity <String > request = new HttpEntity <String, > (sendparm, headers);
restTemplate.postForObject(url, request, String.class);
Server Side
@RequestMapping(value="/test",consumes=MediaType.APPLICATION_PLAIN_TEXT,METHOD="POST")
public void testPost(@ResponseBodyHttpEntity<String>request){
String receiveParam=request.getBody;
}
I wanted to pass the contents of the sendparam to the receiveparam on the server side, but it didn't work well.
I would appreciate it if you could let me know which URL you would like to refer to.
@RequestBody is an annotation that declares the type of body.
In other words, you don't need to declare HttpEntity, please declare String directly.
@PostMapping(value="/api/v1/snippet")
publicResponseEntity<?>create(@RequestBodyStringtext){
System.out.println(String.format("Received text is %s., text));
return ResponseEntity.ok().build();
}
I'm sorry I'm not good at Japanese because I'm Malaysian.
© 2024 OneMinuteCode. All rights reserved.