Springmvc+mybatis 问题总结(续ing)

1、java.lang.IllegalStateException: getWriter() has already been called for this response

拦截器中相关代码
@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
            Object arg2) throws Exception {
        response.setCharacterEncoding("UTF-8"); // 设置输出流格式为utf-8
        PrintWriter out = response.getWriter();  //获得输出流
    }

controller相关代码

@ResponseBody
    @RequestMapping(value="/back/updateadmin")
    public Json updateAdmin(UpdateAdmin admin,HttpServletResponse respose) {
        int result = 0;
        Json json = new Json();
        try {
            result = adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(result != 0) {
                json.setMsg("更新管理员操作成功");
                json.setSuccess(true);
            } else {
                json.setMsg("更新管理员失败");
                json.setSuccess(false);
            }
        }
        return json;    
    }

问题原因:
在拦截器中 获得了PrintWriter对象
而在Controller中@responseBody 返回json格式字符串时会自动调用getOutputStream方法。

解决方法:
在拦截器中加上
response.reset();即可

2.SELECT top(1) * FROM edition ORDER BY id DESC
在mysql 数据库下一直报
**You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘FROM edition
ORDER BY id DESC’ at line 1**

问题原因:
并不是所有的数据库都支持top子句

解决方法:
select * from edition order by id limit 1

你可能感兴趣的:(java)