2 shadowspider shadowSpider 于 2016.04.25 13:49 提问

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

``

<!--配置注解映射器和适配器 (这样不需要单独配置json转化器) -->
<mvc:annotation-driven/>

<!--因为在web.xml中配置的url-pattern所有的都由DispatcherServlet处理,而静态文件不需要,所以按如下配置  -->
<!--配置静态文件  -->
<!-- <mvc:default-servlet-handler/> -->
<mvc:resources location="WEB-INF/js/" mapping="/js/**"/>
<mvc:resources location="img/" mapping="/img/**"/>
<mvc:resources location="Public/" mapping="/Public/**"/>

<!-- 扫描Spring注解的配置 -->
<!-- 对于注解的Handler可以单个配置,实际开发中建议使用组件扫描 -->
<!-- 可以扫描controller、service、...这里让扫描controller,指定controller的包 -->
<!-- <context:component-scan base-package="com.tgb.itoo.controller"></context:component-scan> -->
<bean class="com.tgb.itoo.controller.CourseController" />   
<bean class="com.tgb.itoo.controller.IndexController" />
<bean class="org.controller.StudentController" />`

另一个配置文件

${driverClassName}
${url}
${username}
<!-- ${password} -->

<!-- 告知Hibernate的sessionFactory -->    
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <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>
</bean>

<bean id="StudentBean" class="org.dao.StudentDaoImp">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

![图片说明](http://img.ask.csdn.net/upload/201604/25/1461563276_434072.png)

5个回答

CSDNXIAOD
CSDNXIAOD   2016.04.25 13:52

SpringMVC使用注解@ResponseBody返回json中文乱码
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

shadowSpider
shadowSpider 兄台,我问的是解决方法
一年多之前 回复
xiongzirui
xiongzirui   2016.04.25 13:56
shadowSpider
shadowSpider 我没有用maven
一年多之前 回复
xiongzirui
xiongzirui   2016.04.25 14:16

没用maven就直接下载这个jar包导入就可以了啊

xiongzirui
xiongzirui   2016.04.25 14:19
xiumu_
xiumu_   2016.04.25 18:12

应该是Jackson 包没有导入

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!