【Java后端封装数据】常见后端封装数据的格式,用于返回给前端使用(109)

数据格式一:包装 List Map 返回,常用于数据展示;


	// Controller:
	public Result selectRegConfig(@RequestBody String param) {
        try {
            Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
            return Result.success().result(xxxListService.selectRegConfig(paramMap));
        } catch (Exception e) {
            log.error("QuestionListController selectRegConfig is error===:" + e.getMessage(), e);
            return Result.failure("查询失败");
        }
    }
    
	// Service:做了分页展示,所以使用PageInfo>接收
	PageInfo<Map<String, Object>> selectRegConfig(Map<String, Object> paramMap);
	
	// ServiceImpl
	@Override
    public PageInfo<Map<String, Object>> selectRegConfig(Map<String, Object> paramMap) {
        List<Map<String, Object>> list = xxxListMapper.selectRegConfig(paramMap);
        PageInfo<Map<String, Object>> result = new PageInfo<>(list);
        return result;
    }

数据格式二:包装 对象返回;通过封装POJO对象返回数据;


	// POJO:
	@Data
	public class DataCount {
	    private String total;  // 总数
	    private String closed;  // 已关闭
	}
	
	// Controller:
	public Result getDeclaratData(HttpServletRequest request,@RequestBody String param) {
        try {
            Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
            return Result.success().result(xxxListService.getDeclaratData(request,paramMap));
        } catch (Exception e) {
            log.error("QuestionListController getDeclaratData is error===:" + e.getMessage(), e);
            return Result.failure("查询失败");
        }
    }
	
	// Service:
	DataCount getDeclaratData (HttpServletRequest request, Map<String, Object> paramMap);
	
	// ServiceImpl
	@Override
    public DataCount  getDeclaratData(HttpServletRequest request, Map<String, Object> paramMap) {
        DataCount data = new DataCount();
		int total = 0;
		int closed = 0;
        data.setTotal(String.valueOf(total));
        data.setClosed(String.valueOf(closed));
        return data;
    }

数据格式三:包装 Map返回;


	// Controller:
	public Result getRevealData(HttpServletRequest request,@RequestBody String param) {
        try {
            Map<String, Object> paramMap = JsonUtils.readValue(param, Map.class);
            return Result.success().result(xxxListService.getRevealData(request,paramMap));
        } catch (Exception e) {
            log.error("QuestionListController getRevealData is error===:" + e.getMessage(), e);
            return Result.failure("失败");
        }
    }
    
	// Service:
	Map<String, Object> getRevealData (HttpServletRequest request,Map<String, Object> paramMap);
	
	// ServiceImpl
	@Override
    public Map<String, Object> getRevealData (HttpServletRequest request, Map<String, Object> paramMap) {
        Map<String,Object> map = new HashMap<>();
        int A = 0;  
        int B = 0; 
        map.put("A",A);
        map.put("B",B);
        return map;
    }
	

数据格式四:对象,Map,List互相嵌套(随意嵌套封装返回);
如:
1.对象里面嵌套对象,嵌套List等;
2.Map里面嵌套map,嵌套List等;
3.对象和Map互相嵌套等;
返回可以是:List Map ,对象,Map;

总结:包装对象和包装Map从结果上来看是一样的,就看自己使用习惯!

你可能感兴趣的:(java,java,前端,开发语言)