Return literal JSON strings in spring mvc @ResponseBody

This works with Jackson 2 (at least):

@Controller
public class YourController {

    @RequestMapping(..)
    public @ResponseBody Json get() {
        return new Json("{ \"attr\" : \"value\" }");
    }
}

class Json {

    private final String value;

    public Json(String value) {
        this.value = value;
    }

    @JsonValue
    @JsonRawValue
    public String value() {
        return value;
    }
}

Not particularly pretty but works. I only wish Spring supported this:

@RequestMapping(..)
public @JsonRawValue @ResponseBody String get() {
    // ...
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)