返回map形式json数据格式接口

    xiaoxiao2021-04-16  24

    结果:

    如图,返回特定格式的json字符格式,主要使用map添加,不多说:

    result:

    public class Result { private int status; private String message; private Object data; public Result(int status, String message) { this.status = status; this.message = message; } public Result() { this.status = 0; this.message = ""; } /* public boolean isSucc() { return this.status.equals("0"); } */ public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String toString() { // return JSONObject.toJSONString(this); ObjectMapper mapper = new ObjectMapper(); String jsonStr = ""; try{ jsonStr = mapper.writeValueAsString(this); }catch(JsonProcessingException e){ } return jsonStr; } } controller:

    @GetMapping(value = "/currentConfig") public Result getSvCongifurationList(){ Result rs = new Result(); Map map = svConfigurationService.getCurrentConfig(); if (map != null && map.size() > 0) { rs.setData(map); } else { rs.setStatus(10000); rs.setMessage("当前版本没有配置信息"); } return rs; } service:

    public Map getCurrentConfig(){ Integer version = svConfigurationDao.getNewestVersion(); List<SvConfiguration> config = svConfigurationRepository.findByVersion(version); if (config.size() > 0) { Map map = new HashMap(); Map mapVersion = new HashMap(); for(int i=0;i<config.size();i++){ map.put(config.get(i).getKey(),config.get(i).getValue()); } mapVersion.put("config",map); mapVersion.put("version",version); return mapVersion; } return null; }

    转载请注明原文地址: https://ju.6miu.com/read-672539.html

    最新回复(0)