映射宁静的Ajax请求去春来 [英] Mapping restful ajax requests to spring

查看:204
本文介绍了映射宁静的Ajax请求去春来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这块code:

@RequestMapping(value = "/test.json", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody Object[] generateFile(@RequestParam String tipo) {
    Object[] variaveis = Variavel.getListVariavelByTipo(tipo);
    return variaveis;
}

据我所知,应该采取请求test.json TIPO = H,并返回的JSON再presentation Variavel [],但是当我提出这样的要求,我得到:?

As far as I know it should take a request to test.json?tipo=H and return the JSON representation of Variavel[], however when I make such request I get:

HTTP状态406 -

HTTP Status 406 -

型状态报告

消息

确定该请求descriptionThe资源只能产生响应与根据接受报头()的要求不被接受的特点。

descriptionThe resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()

通过下面的功能,我可以得到预期的JSON:

By using the following function I can get the expected json:

@RequestMapping(value = "/teste.json")
public void testeJson(Model model, @RequestParam String tipo) {
    model.addAttribute("data", Variavel.getListVariavelByTipo("H"));
}

我在做什么错了?

What I'm doing wrong?

推荐答案

@RequestBody / @ResponseBody 注解唐'T使用普通视图解析器,他们用自己的 HttpMessageConverter 秒。为了使用这些注解,你应该配置这些转换器 AnnotationMethodHandlerAdapter上,如<一个描述href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestbody">the参考(你可能需要 MappingJacksonHttpMessageConverter )。

@RequestBody/@ResponseBody annotations don't use normal view resolvers, they use their own HttpMessageConverters. In order to use these annotations, you should configure these converters in AnnotationMethodHandlerAdapter, as described in the reference (you probably need MappingJacksonHttpMessageConverter).

这篇关于映射宁静的Ajax请求去春来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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