ResponseBody中返回文字JSON字符串

ResponseBody中返回文字JSON字符串

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

问题描述

我将数据库中的对象存储为JSON字符串。我想制作一个暴露这些字符串的REST服务。然而,当我编写我的方法时,我回来的字符串会引用它们的引号。例如,我已经包含了一个返回String的方法,

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 \}当我真正想要发生的是{a:1,b:foo}。我认为String作为返回类型可能是问题所在,但我还能做些什么呢?包装类执行相同的操作:

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

谢谢

编辑:如下所示,似乎字符串是双重编码。在我的配置中注释掉这两个类可以解决这个问题。但是,我仍然有其他地方我想要返回对象,并希望保持那些运行通过我知道在哪里配置的常见序列化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() {
    // ...
}

HTH,
Jukka

HTH,Jukka

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

07-26 06:05