java后台HTTP请求返回json格式数据
实现
枚举类ResponseState(所有自定义的状态码写在里面)
package com.xxx.common.enums;
public enum ResponseState {
//状态码添加在这里
/**
* 系统繁忙,此时请开发者稍候再试
*/
REP_ERROR("-1", "系统繁忙,此时请开发者稍候再试"),
/**
* 请求成功
*/
REP_SUCCESS("0", "请求成功"),
/**
* Session无效
*/
REP_Session_ERROR("40001", "Session无效"); //最后是分号结尾
public String state;
public String message;
ResponseState(String state, String message) {
this.state = state;
this.message = message;
}
@Override
public String toString() {
return name();
}
}
工具类ResponseData
package com.xxx.common.utils;
import com.xxx.common.enums.ResponseState;
import java.util.HashMap;
import java.util.Map;
public class ResponseData {
public static Map<String, Object> result(String state, String message, Object data){
Map<String, Object> responseMap = new HashMap<String, Object>();
responseMap.put("state", state);
responseMap.put("message", message);
responseMap.put("data", data);
return responseMap;
}
public static Map<String, Object> success(){
return result(ResponseState.REP_SUCCESS.state, ResponseState.REP_SUCCESS.message, "");
}
public static Map<String, Object> success(Object data){
return result(ResponseState.REP_SUCCESS.state, ResponseState.REP_SUCCESS.message, data);
}
public static Map<String, Object> error(){
return result(ResponseState.REP_ERROR.state, ResponseState.REP_ERROR.message, "");
}
public static Map<String, Object> error(Object data){
return result(ResponseState.REP_ERROR.state, ResponseState.REP_ERROR.message, data);
}
}
调用
controller
package com.xxx.platform.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xxx.common.enums.ResponseState;
import com.xxx.common.utils.ResponseData;
/**
* 文章相关接口
*/
@Controller
@RequestMapping("/article")
public class ArticleController{
//注入redis模板
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private ArticleService articleService;
/**
* 获取文章列表
* @param 会话密钥sessionId
* @param 当前页数page.currentPage、每页显示数量page.pageSize(非必填,默认5)
* @param 文章名称article_title(非必填)
*
* @return 文章总数total、总页数pageTotal、当前页文章数据
*/
@RequestMapping(value="/getListArticle",produces="application/json; charset=utf-8")
@ResponseBody
public Map<String, Object> getListArticle(ArticleListCustom articleListCustom,String sessionId,Integer doctor_id) {
//从缓存中获取open_id
Object wxSessionObj = redisTemplate.opsForValue().get(sessionId);
if(null == wxSessionObj){
//这里调用
return ResponseData.result(ResponseState.REP_Session_ERROR.state, ResponseState.REP_Session_ERROR.message, "test看一下");
}
articleListCustom.setDoctor_id(doctor_id);
return articleService.getListArticle(articleListCustom);
}
}
serviceImpl
package com.doctorrbq.platform.service.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xxx.common.enums.ResponseState;
import com.xxx.common.utils.ResponseData;
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService{
@Autowired
private ArticleMapper articleMapper;
/*
* 获取文章列表
*/
@Override
public Map<String, Object> getListArticle(ArticleListCustom articleListCustom) {
Map<String, Object> responseData = new HashMap<String, Object>();
// 获取分页查询所需要的总文章条数
int total = articleMapper.getTotal(articleListCustom);
int pageSize = articleListCustom.getPage().getPageSize();// 得到每页数据个数(默认为10)
int pageTotal = (int) Math.ceil(total / (double) pageSize); // 得到页数,给前台返回
// 分页查询结果,给前台返回
List<ArticleListCustom> articlelist = articleMapper.getListArticle(articleListCustom);
// 返回数据总条数
responseData.put("total", total);
// 返回数据列表
responseData.put("articlelist", articlelist);
// 返回页数
responseData.put("pageTotal", pageTotal);
return ResponseData.success(responseData);
//return ResponseData.result(ResponseState.REP_SUCCESS.state, ResponseState.REP_SUCCESS.message, responseData)
}
}
效果
{"message":"Session无效","data":"test看一下","state":"40001"}