0

In my spring-mvc application, the client requests with a key and I lookup the associated value in Couchbase. From Couchbase I get a json object as a String, as my application doesn't need to do anything with it I just want this to be written out.

But Spring sees that I want to write a String, and passes it to jackson which then writes it as a json string - adding in quotation marks and escaping the internals.

Simplest example:

@RequestMapping(value = "/thing", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public DeferredResult<String> getThing() {
    final DeferredResult<String> result = new DeferredResult<>();
    // in future
    result.setResult("{}");
    return result;
}

Returns: "{}"

I'm thinking of making a serialised json wrapper and a custom serialiser for jackson to just output it's contents.

2 Answers 2

1

I think you could use org.springframework.http.ResponseEntity

@RequestMapping(value = "/example", headers = "Accept=application/json")
@ResponseBody
@Transactional
public ResponseEntity<String> example(@RequestParam(value = "documentId", required = true) Integer documentId) {

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");

    SomeDTO result = SomeDTO.findDocument(documentId);
    if (result == null) {
        return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
    }

    return new ResponseEntity<String>(result.toJson(), headers,HttpStatus.OK);
}

beside, you could use Flexjson to tranfer Oject to Json String.

Sign up to request clarification or add additional context in comments.

Comments

0

I added the StringHttpMessageConverter with application/json as a supportedMediaType:

    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="supportedMediaTypes" value = "application/json" />
        </bean>
    </mvc:message-converters>

This did get the String responses output as-is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.