12

Let's take a look at the following simple test controller (Used with Spring 4.0.3):

@RestController
public class TestController
{
    @RequestMapping("/getList")
    public List<String> getList()
    {
        final List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        return list;
    }

    @RequestMapping("/getString")
    public String getString()
    {
        return "Hello World";
    }
}

In theory both controller methods should return valid JSON. Calling the first controller method indeed does return the following JSON array:

$ curl -i -H "Accept: application/json" http://localhost:8080/getList
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8

["1","2"]

But the second controller method returns the string without quotes which is not a valid JSON string:

$ curl -i -H "Accept: application/json" http://localhost:8080/getString
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8

Hello World

Why is that so? Can it be configured? Is it a bug? Or a feature I don't understand?

kayahr
  • 20,913
  • 29
  • 99
  • 147

4 Answers4

8

When you return a String object, Spring MVC interprets that as the content to put in the response body and doesn't modify it further. If you're wanting an actual string to be the JSON response, you'll need to either quote it yourself or run it through Jackson explicitly.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Sounds quite strange to me. According to the `Accept` header in the request the only valid response must be of content-type `application/json` so Spring has to use Jackson for the answer and Jackson should produce valid JSON. In this case the response is simply invalid. It has content-type `application/json` but was not converted to JSON. – kayahr May 10 '14 at 13:36
  • 5
    @kayahr But what if you wanted to produce your own JSON string without Jackson (maybe a very simple string concatenation)? Then Spring would have to re-parse your response just to check up on you. `ResponseBody` means `ResponseBody`, which is ultimately a sequence of bytes, and Spring only performs conversions if it can't match up the Java types in question; `String` doesn't need conversion. – chrylis -cautiouslyoptimistic- May 10 '14 at 13:44
5

You can remove the StringHttpMessageConverter which is registered before the jackson converter, - like mentioned in the comment.

/**
 * Unregister the default {@link StringHttpMessageConverter} as we want
 * Strings to be handled by the JSON converter.
 *
 * Our MappingJackson2HttpMessageConverter will deal with strings.
 *
 * @param converters
 *            List of already configured converters
 */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.removeIf(converter -> converter instanceof StringHttpMessageConverter);
}
d0x
  • 11,040
  • 17
  • 69
  • 104
  • 1
    Btw, this can be written as a nice one-liner in java 8: `converters.removeIf(converter -> converter instanceof StringHttpMessageConverter);` – BeneStr Sep 11 '18 at 12:28
0

If you want to return a String object, Spring MVC interprets that as the content to put in the response body and doesn't modify it further. If you're wanting an actual string to be the JSON response, you'll need to either quote it yourself or run it through Jackson explicitly.

@RestController
public class TestController
{
   @RequestMapping("/getString")
   public String getString()
  {
    return JSONObject.quote("Hello World");
  }
}
Gopal
  • 580
  • 8
  • 26
0

Here are the steps that I did to achieve this :

  1. Add dependency in pom file:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.3</version>
    </dependency>
    
  2. Put @ResponseBody annotation on your method like this:

    @RequestMapping(value = "/getCountries", method = RequestMethod.GET)    
    @ResponseBody    
    public List<Country> getCountries() {    
        return countryDAO.list();    
    }
    
S'chn T'gai Spock
  • 1,203
  • 18
  • 16