开发者社区> 问答> 正文

SpringMVC使用注解解析json总是报415错误

前端使用angularjs的$resource发送
这是方法:
@RequestMapping(value = "/studentTest")
public class StudentController {

/**
 * 测试
 */
@RequestMapping(value = "/save",method = RequestMethod.POST,consumes = "application/json")
@ResponseBody
public HashMap<String,Object> save(@RequestBody Student student){
    HashMap<String,Object> hashMap = new HashMap<String,Object>();

    ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc.xml");
    StudentDao studentDao = (StudentDao)ctx.getBean("StudentBean");

    if(studentDao.save(student)){
        hashMap.put("error", 0);
        hashMap.put("msg", "插入测试成功");
    }else{
        hashMap.put("error", 1);
        hashMap.put("msg", "插入测试失败");
    }
    return hashMap;
}

}

这是前端angularjs的$resource
TestApp.factory('TestService',function($resource){
return $resource(projectName + '/student/courseList',{},{
insert: {
url: projectName + '/studentTest/save',
method: 'POST',
isArray: false,

}

});

});
$scope.registerInfo = [{
'snumber': 'qubxxk',
'sname': "接口测试",
'sage': 10
}]

TestService.insert($scope.registerInfo,function(res){
    if(res.error === 0){
        if(res.msg !== null){
            console.log(res.msg);
        }else{
            console.log("插入测试成功");
        }
    }else{
        if(res.msg !== null){
            console.log(res.msg);
        }else{
            console.log("插入测试失败");
        }
    }
    console.log(res);
    //恢复按钮状态
    $scope.test_btn = '插入测试';
    $scope.insert_disabled = false;
},function(){
    console.log("api连接失败");
    $scope.test_btn = '插入测试';
    $scope.insert_disabled = false;
});

下面是配置文件springmvc.xml

``












`

另一个配置文件

${driverClassName}
${url}
${username}


<property name="dataSource" ref="dataSource"></property>      
<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="current_session_context_class">thread</prop>
    </props>
</property>
<property name="mappingResources">
    <list>
        <value>org/bean/Student.hbm.xml</value>
    </list>
</property>

<property name="sessionFactory">
    <ref bean="sessionFactory"/>
</property>

图片说明

展开
收起
杨冬芳 2016-07-07 18:27:35 3170 0
1 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像