spring boot 项目集成layui 关于table 渲染数据的时候一直报数据接口请求异常:error的处理
1.问题描述
官方文档给出的table需要的数据格式
一个json对象
2.我的后端编写为
package com.lin.hr.org.controller; import com.lin.hr.commen.JSONUtils; import com.lin.hr.org.entity.Org; import com.lin.hr.org.service.IOrgService; import com.lin.hr.person.entity.Person; import com.lin.hr.person.service.IPersonService; import com.lin.hr.user.entity.User; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <p> * 前端控制器 * </p> * * @author hailing * @since 2020-02-22 */ @Controller @RequestMapping("/org") public class OrgController { @Autowired private IPersonService personService; @Autowired private IOrgService orgService; /** * 获取机构的列表数据 * @param request * @return */ @RequestMapping("/list") public JSONObject toOrg(HttpServletRequest request, @RequestParam(value="page") String page, @RequestParam (value="limit") String limit){ JSONObject res = null; try{ //退出系统:销毁session里面存放的用户,跳转到登录页面 HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); Person person = personService.selectById(user.getPersonId()); String orgId = person.getOrgId(); List<Org> orgList = null; Integer limit1 = Integer.valueOf(limit); Integer page1 = Integer.valueOf(page); Integer start = (page1-1)*limit1; Map map = new HashMap(); map.put("start",start); map.put("limit",limit1); //如果进入管理页面,那么这个人肯定是管理员,那么这个人的id肯定=该机构的负责人id //1.如果这个人是超级管理员,就可以看到所有机构,机构的id为-1, if (orgId.equals("-1")){ //分页查询列表数据 orgList = orgService.selectOrgList1(map); }else { // 2.如果为机构负责人,那该负责人就只能看到自己机构的情况 map.put("orgId",orgId); orgList = orgService.selectOrgList2(map); } //将list类型的数据转换成json数据 JSONArray datas = JSONUtils.toJSONArray(orgList); res = new JSONObject(); res.put("status",0);//解析接口状态 res.put("message","");//解析提示文本 res.put("total",1);//解析数据长度 res.put("data",datas);//解析数据列表 }catch (Exception e){ e.printStackTrace(); } return res; } }
前端没有问题
3.解决方式
controller有两种返回数据的方式:1.返回页面,使用@Controller注解 2.返回数据,使用@ResponseBody.
很明显这个controller我需要返回一个json数据所以要用第二种注解。
加上注解之后就能解决问题了。
还是springboot项目经验不足啊~~~~~
希望我遇到的问题能够帮到你们,好运。