Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make RestController handle json Strings properly

Tags:

java

json

spring

As mentioned for example in RFC 7159, strings etc primitive types are valid json messages on their own. But the string has to be encased in double quotes.

So, string:

test

in json is:

"test"

If I send a properly quoted POST body

"test"

to the following code

@RestController
@RequestMapping("test")
public class TestController{
    @RequestMapping(method=RequestMethod.POST)
    public ResponseEntity<?> userLogout(@RequestBody final String input) {
        System.out.println(input);
        return new ResponseEntity<>("OK", HttpStatus.OK);
    }
}

the value of input variable is

"test"

instead of

test

which would be the correct value.

Also the output of the request is

OK

instead of

"OK"

which would be the correct output.

Any ideas how to force Spring to handle the strings properly?

The request does have the correct header:

Content-Type: application/json; charset=UTF-8

And adding produces="application/json" to the RequestMapping annotation didn't help.

(Just a side note: If you use retrofit or Gson for communication, they do handle strings properly.)

like image 416
Groosa Avatar asked Jun 28 '16 18:06

Groosa


People also ask

How consume JSON from RESTful web service in spring boot?

Create a simple Spring Boot web application and write a controller class files which is used to redirects into the HTML file to consumes the RESTful web services. We need to add the Spring Boot starter Thymeleaf and Web dependency in our build configuration file. For Maven users, add the below dependencies in your pom.

How pass JSON object in post request in spring boot?

Send JSON Data in POST Spring provides a straightforward way to send JSON data via POST requests. The built-in @RequestBody annotation can automatically deserialize the JSON data encapsulated in the request body into a particular model object. In general, we don't have to parse the request body ourselves.


Video Answer


1 Answers

String is a special type for Spring MVC.

When trying to produce an argument for a @RequestBody annotated parameter, Spring MVC choose from a list of default or custom HttpMessageConverter implementations. Two of these are relevant for your example (ordered this way): StringHttpMessageConverter and MappingJackson2HttpMessageConverter (or the Gson equivalent).

StringHttpMessageConverter supports all media types and will therefore be used to read the raw text from your request, which is "test", and provide that value as an argument.

You can provide your own list of HttpMessageConverter objects in the order you want. If you put MappingJackson2HttpMessageConverter first, then it would be able to parse your JSON as a String and produce the String argument value test.

If you don't want to go through those hoops, you can also change your parameter to the Jackson type corresponding to a JSON string, TextNode and then retrieve its value.

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> userLogout(@RequestBody final TextNode input) {
    System.out.println(input.asText());
    return new ResponseEntity<>("OK", HttpStatus.OK);
}

The same applies for the response. The value "OK" is a String and will be handled with a StringHttpMessageConverter. Use a TextNode or reorder your HttpMessageConverter list.

return new ResponseEntity<>(new TextNode("OK"), HttpStatus.OK);
like image 84
Sotirios Delimanolis Avatar answered Nov 14 '22 22:11

Sotirios Delimanolis