17

I'm using Spring 3.1 and I have a handler that should return a String value. Here's how my handler looks like:

@RequestMapping(value = TEST_HANDLER_PATH, method = RequestMethod.POST)
public ResponseEntity<String> handleTest(HttpServletRequest request,
    @RequestParam("parma1") String param) throws Exception {
    String ret = ...
    ...
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "text/plain;charset=utf-8");
    return new ResponseEntity<String>(ret, headers, HttpStatus.CREATED);
}

I also tried annotating method with @ResponseBody with return ret; at the end.

In both cases, when I hit the service, I get extra quotes around String value (e.g. "This is a test"). I'm guessing this is due to message conversion. That's why I tried defining Content-Type header, to hit StringHttpMessageConverter explicitly, to no avail.

mkvcvc
  • 1,515
  • 1
  • 18
  • 41

4 Answers4

23

Had the same problem.

Just make sure you register a org.springframework.http.converter.StringHttpMessageConverter as well as your Jackson one so that Strings are treated literally and not attempted to be converted to JSON (with extra quotes).

Just instantiate with default constructor or constructor with your preferred Charset. The media types should be set for you with the standard internal defaults. If you're configuring via code extending WebMvcConfigurerAdapter then you just add the converters in the configureMessageConverters(List<HttpMessageConverter<?>> converters) method.

Matt Byrne
  • 4,908
  • 3
  • 35
  • 52
  • 6
    This worked for me. Also `StringHttpMessageConverter` must be added to `List>` before any other converter. The order matters, when you configure them explicitly – Ramanujan R Nov 04 '17 at 08:37
  • 3
    Thank you both. Order does matter. If you are using `extendMessageConverters` you will need to explicitly add the `StringHttpMessageConverter` to index 0. – JMess Oct 08 '18 at 23:29
0

In my case, I had over-engineered =)

Had introduced a converter for bean's toString Operations like this:

class SerializableToString implements Converter<Serializable, String>

restricting that (only to my beans), resolved the issue X)

Note: debugging with a breakpoint @ org.springframework.core.convert.support.GenericConversionService.getConverter helped.

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
0

In a related scenario, I had an IntegrationFlow for a GET that incorrectly requested a transform. Basically the target service would receive the @PathVariable as a quote escaped string

        return IntegrationFlows.from("getThing")
        .transform(Transformers.toJson())
        .handle(

The .transform(Transformers.toJson()) was forcing the strings to be escaped in the URI, so simply removing it - it shouldn't have been there - fixed the issue.

Mike Holdsworth
  • 1,088
  • 11
  • 12
-1

Turns out there was a JSON message converter registered in one of the imports.

mkvcvc
  • 1,515
  • 1
  • 18
  • 41
  • 2
    Did you remove the JSON message converter? What if you don't want to remove the message converter? – John S Feb 07 '13 at 19:35
  • You can target specific converter using corresponding MIME type in request/response headers. Taking a look under the hood is what helped me out. – mkvcvc Feb 07 '13 at 20:43