Passing the string string in server-client communication in the Rest API

Asked 2 years ago, Updated 2 years ago, 89 views

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.

java spring

2022-09-29 22:24

1 Answers

@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.


2022-09-29 22:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.