在 spring mvc @ResponseBody 中返回文字 JSON 字符串 [英] Return literal JSON strings in spring mvc @ResponseBody

查看:114
本文介绍了在 spring mvc @ResponseBody 中返回文字 JSON 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将对象作为 JSON 字符串存储在我的数据库中.我想制作一个公开这些字符串的 REST 服务.然而,当我编写我的方法时,我返回的字符串会转义它们的引号.例如,我包含了一个返回字符串的方法,

I am storing objects in my database as JSON strings. I want to make a REST service that exposes these strings. When I write my methods however, the strings I get back have their quotes escaped. For example, I have included a method that returns a String,

@RequestMapping(value = "test", method = RequestMethod.GET)
public @ResponseBody
String getTest() {
    return "{"a":1, "b":"foo"}";
}

但是当我在浏览器中调用这个方法时,当我真正想要发生的是 {"a": 1, "b": "foo"}.我认为字符串"作为返回类型可能是问题所在,但我还能做什么?包装类做同样的事情:

but when I call this method in the browser I get a back "{"a":1, "b":"foo"}" when what I really want to happen is {"a": 1, "b": "foo"}. I think "String" as the return type is likely the problem, but what else can I do? A wrapper class does the same thing:

{
  "value" : "{"a":1, "b":"foo"}"
}

我可以序列化它然后返回对象,但这似乎有点荒谬.这是我的配置文件的可能相关部分:

I could serialize it and then return the object, but that seems a bit ridiculous. Here is a possibly the relevant portion of my configuration file:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    converters.add(mappingJacksonHttpMessageConverter());
}

@Bean
MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter() {
    MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJacksonHttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    mappingJacksonHttpMessageConverter.setObjectMapper(objectMapper);
    mappingJacksonHttpMessageConverter.setPrettyPrint(true);
    return mappingJacksonHttpMessageConverter;
}

谢谢

正如下面所建议的,似乎字符串被双重编码.在我的配置中注释掉 2 个类可以解决这个问题.但是,我仍然有其他地方想要返回对象,并希望通过我知道在哪里配置的通用序列化 bean 运行这些对象.所以我认为我的选择是:a) 自己做所有的连载.所有方法都返回字符串,那些已经是 JSON 的返回自己,那些是对象的都返回 JSONUtil.toJson(object).我不喜欢这种方法,但我知道它会奏效.b) 使用一个看起来像这样的包装类:

as was suggested below, it seems the string is being double encoded. Commenting out the 2 classes in my configuration fixes this issue. However, I still have other places where I want to return Objects and would like to keep those running through that common serializing bean that I know where to configure. So I see my options as: a) Do all the serializing myself. All methods return Strings, and those that are already JSON return themselves, and those that are objects all return JSONUtil.toJson(object). I don't love this approach, but I know it will work. b) Use a wrapper class that looks kind of like:

public static class Wrapper {

    @JsonRawValue
    private final String value;
}

这在前面导致了一个尴尬的值",尽管它没有实际意义.

This leads to an awkward "value" at the front though that has no real meaning.

基本上我想要的是@JsonRawValue,但是让它在RequestMapping 方法而不是属性上工作.想法?意见?其他建议?

Basically what I want is @JsonRawValue, but to have it work on RequestMapping methods instead of properties. Thoughts? Opinions? Other suggestions?

推荐答案

这适用于 Jackson 2(至少):

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;
    }
}

不是特别漂亮,但很管用.我只希望 Spring 支持这个:

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

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

这篇关于在 spring mvc @ResponseBody 中返回文字 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆