spring mvc restcontroller返回json字符串 [英] spring mvc restcontroller return json string

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

问题描述

我有一个Spring MVC控制器,使用以下方法:

I have a Spring MVC controller with the following method:

@RequestMapping(value = "/stringtest", method = RequestMethod.GET)
public String simpletest() throws Exception {
    return "test";
}

这位于控制器内部,如下所示:

This sits inside a controller that starts like this:

@RestController
@RequestMapping(value = "/root")
public class RootController

当我调用其他返回对象的方法时,这些对象被Jackson序列化为JSON。但是这个返回String的方法不会转换为JSON。如果不清楚,这是一个使用curl的例子:

When I call other methods that return objects, those objects are serialized by Jackson into JSON. But this method which returns a String is not converted to JSON. In case it's not clear, here's an example using curl:

$curl http://localhost:8080/clapi/root/stringtest 
test

所以问题是没有任何引号的test不是JSON字符串,但我的REST客户端期待一个字符串。我期望curl命令显示带有引号的字符串,所以它是合法的JSON:

So the problem is that "test" without any quotes is not a JSON string, but my REST client is expecting a string. I expected the curl command to show that string with quotes around it so it's legal JSON instead:

"test"

我使用的是Spring WebMVC 4.1.3和Jackson 2.4.3。我试过在RequestMapping中添加一个produce属性,说它应该返回JSON。在这种情况下,发回的Content-Type属性是application / json,但仍未引用测试字符串。

I am using Spring WebMVC 4.1.3 and Jackson 2.4.3. I have tried adding a "produces" attribute to the RequestMapping to say it should return JSON. In this case, the Content-Type attribute sent back is "application/json" but still the test string is not quoted.

我可以通过调用JSON库来解决此问题通过Java String转换为JSON,但似乎Spring MVC和Jackson通常会自动执行此操作。但不知何故,他们并没有这样做。任何想法,我可能配置错误,只是测试回来而不是测试?

I could workaround this by calling a JSON library to convert by Java String to JSON, but it seems like Spring MVC and Jackson generally do this automatically. Yet somehow they are not doing it in my case. Any ideas what I might have configured wrong to be getting just test back instead of "test"?

推荐答案

事实证明,当你使用 @EnableWebMvc 注释,它默认打开一堆http消息转换器。列表中的第二个是 StringHttpMessageConverter ,文档说明将应用于 text / * 内容类型。但是,在使用调试器后,它适用于 * / * 内容类型的String对象 - 显然包括 application / json

It turns out that when you use the @EnableWebMvc annotation that it turns on a bunch of http message converters by default. The second one in the list is the StringHttpMessageConverter which the documentation says will be applied for text/* content types. However, after stepping through with the debugger, it applies to String objects for */* content types- which obviously includes application/json.

MappingJackson2HttpMessageConverter 负责 application / json 内容类型进一步列在此列表中。因此,对于除String之外的Java对象,将调用此对象。这就是它为Object和Array类型工作的原因,但不是String-尽管使用produce属性设置 application / json 内容类型的好建议。尽管该内容类型是触发此转换器所必需的,但String转换器首先抓住了这个作业!

The MappingJackson2HttpMessageConverter which is responsible for application/json content types is further down on this list. So for Java objects other than String, this one gets called. That's why it was working for Object and Array types, but not String- despite the good suggestions of using the produces attribute to set application/json content type. Although that content type is necessary to trigger this converter, the String converter was grabbing the job first!

因为我正在扩展 WebMvcConfigurationSupport 用于其他一些配置的类,我重写了以下方法以将Jackson转换器放在第一位,这样当内容类型为 application / json 时,我将使用这个而不是字符串转换器:

As I was extending the WebMvcConfigurationSupport class for some other configuration, I overrode the following method to put the Jackson converter first so when the content-type is application/json then this one will be used instead of the String converter:

@Override
protected void configureMessageConverters(
        List<HttpMessageConverter<?>> converters) {
    // put the jackson converter to the front of the list so that application/json content-type strings will be treated as JSON
    converters.add(new MappingJackson2HttpMessageConverter());
    // and probably needs a string converter too for text/plain content-type strings to be properly handled
    converters.add(new StringHttpMessageConverter());
}

现在当我从curl调用测试方法时,我得到了所需的test输出而不仅仅是 test ,所以期待JSON的角度客户端现在很高兴。

Now when I call the test method from curl I get the desired "test" output instead of just test, so the angular client which is expecting JSON is now happy.

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

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