backbone ajax post json 请求 spring mvc 4.1.4 无法接收参数

使用backbone 模型的save方法,发送post请求

model.save(model.toJSON())


后端使用java 的springmvc框架 (4.1.4),

无法解析json注入对象,代码如下:

 @RequestMapping(value = "/add", method = RequestMethod.POST, consumes = "application/json", produces = "application/json",headers ={"Accept=application/json"})
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public String add(@RequestBody Activity activity, HttpServletRequest request, HttpServletResponse response) {

上面加的注解是看了网上许多资料加的,但是还不是管用,还有返回415的错误。

另外还配置了jakson,也不好用,代码如下:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="atom" value="application/atom+xml"/>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
            </list>
        </property>
    </bean>

求大神解答,在线等,急!!!

更新:在线等,急!!!

阅读 7.2k
4 个回答

自己回答,问题已解决。

spring mvc 不需要修改任何配置, 使用的backbone 版本为 1.0.0

修改backbone emulateJSON = true
//添加判断条件  !options.emulateJSON &&
 // Ensure that we have the appropriate request data.
            if (!options.emulateJSON && options.data == null && model && (method === "create" || method === "update" || method === "patch")) {
                params.contentType = "application/json";
                params.data = JSON.stringify(options.attrs || model.toJSON(options));
            }
            // For older servers, emulate JSON by encoding the request into an HTML-form.
            if (options.emulateJSON) {
                params.contentType = "application/x-www-form-urlencoded";
                params.data =  model.toJSON();//这一行做了修改
            }
新手上路,请多包涵

同求!!!在线等!!!

写个filter,在spring mvc前面拦截一下, 看requst里的内容,看看有没有什么发现

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题