0

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

{} && 
{
    "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 方法中未定义的值。

我的请求映射:

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

1 回答 1

3

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

"指示此视图的 JSON 输出是否应以 "{} && " 为前缀。默认为 false。"

springsource 文档

于 2013-03-11T13:20:47.393 回答