Spring Controller生成的无效JSON内容(使用Jackson库) [英] Invalid JSON content generated by Spring Controller (using Jackson library)

查看:136
本文介绍了Spring Controller生成的无效JSON内容(使用Jackson库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些我不知道的原因,当向我的Spring控制器发出请求时,它返回一个无效的JSON值。我正在使用Jackson来映射我的JSON对象。这是我提出请求时返回的数据:

For some reason unknown to me, when making a request to my Spring controller it is returning an invalid JSON value. I'm using Jackson to map my JSON object. This is the data being returned when I make the request:

{} && 
{
    "registros": [
        {
            "id": 251,
            "matricula": "32849923",
            "dadoPessoal": {
                "nome": "Testando",
                "email": "tiare@terra.com.br",
                "telefone": "1235324812",
                "celular": "123832911",
                "foto": null,
                "salario": 3829
            },
            "status": true,
            "nascimento": {
                "dataNascimento": 1417485600000,
                "nascionalidade": "Brasil",
                "localNascimento": "SP"
            },
            "documentoPessoal": {
                "rg": "8329892332",
                "orgaoEmissor": "SSP/SP",
                "dataEmissao": 1417485600000,
                "cpf": "016.015.XXX-XX",
                "tituloEleitor": "adw91021",
                "zonaDeVoto": "91aa",
                "sessaoVoto": "2a",
                "enderecoVoto": "adw"
            },
            "dataAdmissao": 1361674800000,
            "dataDesligamento": null,
            "version": 0
        }
    ],
    "total": 1
}

这里的问题是某些无效字符{}&&正被添加到JSON的开头。我不明白的是怎么样?它添加了我在Spring方法中没有定义的值。

The problem here is that somehow invalid characters "{} &&" are being added to the beginning of the JSON. What I'm not understanding is how? Its adding these values that are not defined anywhere in my Spring method.

我的请求映射:

@Override
@RequestMapping(value = { "/", "" }, method = RequestMethod.GET)
public ModelAndView index() {
    ModelAndView view = new ModelAndView(VIEW_INDEX);

    view.addObject("registros", service.findAll());
    view.addObject("total", service.findAll().size());
    return view;
}

杰克逊配置:

/**
 * @return MappingJacksonJsonView
 */
@Bean
public MappingJacksonJsonView mappingJacksonJsonView() {
    MappingJacksonJsonView mappingJacksonJsonView = new MappingJacksonJsonView();
    mappingJacksonJsonView.setContentType("application/json");
    mappingJacksonJsonView.setObjectMapper(this.objectMapper());
    mappingJacksonJsonView.setEncoding(JsonEncoding.UTF8);
    mappingJacksonJsonView.setPrefixJson(true);
    return mappingJacksonJsonView;
}

/**
 * @return ContentNegotiatingViewResolver
 */
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();

    // Tiles
    viewResolvers.add(this.tileViewResolver());

    // Views 
    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(this.mappingJacksonJsonView());

    ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();

    contentNegotiatingViewResolver.setViewResolvers(viewResolvers);
    contentNegotiatingViewResolver.setDefaultViews(defaultViews);
    contentNegotiatingViewResolver.setOrder(0);

    return contentNegotiatingViewResolver;
}


推荐答案

谢谢大家。我发现了错误!!
当我开始阅读javadoc时,我发现了这个:

Thanks you all. I found the error!! When i started to read the javadoc i found this:

表示此视图的JSON输出是否应该以{}&& 。默认为假。

"Indicates whether the JSON output by this view should be prefixed with "{} && ". Default is false."

springsource docs

这篇关于Spring Controller生成的无效JSON内容(使用Jackson库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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