返回json格式 不忽略null字段
Posted 疯子加天才
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了返回json格式 不忽略null字段相关的知识,希望对你有一定的参考价值。
11 回复
@wendal 是加到对应字段还是实体,还是哪?
@wendal 实体类上
@wendal 例如Jackson的@JsonInclude
@wendal 个人觉得nutz需要关注一下https://nutz.cn/yvr/t/7fmfpb9oguivrqck7add0u7i1r这个问题
@fengjidong1991 上代码
@IocBean
@Ok("json:full")
@Fail("http:500")
@At("/facManageCar")
public class FacManagerCarModule extends BaseModule{
@At
public Object queryFacManageCar(FacManageCarReq req){
FacManageCarResp resp = new FacManageCarResp();
Pager pager = dao.createPager(req.page,20);
Cnd where = Cnd.NEW();
where.and("facId","=",req.factoryId);
if(req.carStatus==1){
where.and("carStatus","=",false);
}else if(req.carStatus==2){
where.and("carStatus","=",true);
}
//车牌号
if(Strings.isNotBlank(req.carLicence)){
where.and("carLicence","like","%"+req.carLicence+"%");
}
//车主手机号
if(Strings.isNotBlank(req.cellPhone)){
where.and("cellPhone","like","%"+req.cellPhone+"%");
}
//车主姓名
if(Strings.isNotBlank(req.realName)){
where.and("realName","like","%"+req.realName+"%");
}
//保养到期
if(req.carNextTime!=null){
where.and("carNextTime","<=",req.carNextTime);
}
//保险到期
if(req.insuranceEndDate!=null){
where.and("insuranceEndDate","<=",req.insuranceEndDate);
}
//年检到期
if(req.checkEndDate!=null){
where.and("checkEndDate","<=",req.checkEndDate);
}
//根据字段名字排序
if (req.sortType == null) {
where.orderBy("createTime", "desc");
} else {
where.orderBy("convert (" + req.sortType.getName() + " using gbk)",
"" + req.sortType.getSorts() + "");
}
List <FacManageCarView> fmcvs = dao.query(FacManageCarView.class, where,pager);
int count =dao.count(FacManageCarView.class,where);
if(fmcvs==null||fmcvs.size()==0){
resp.requestCode=-1;
return resp;
}
List<FacManageCarEntity> fmces = new ArrayList<FacManageCarEntity>();
List<Dictionary> dcs = dao.query(Dictionary.class,Cnd.where("type","=","car_state"));
for(int i=0;i<fmcvs.size();i++){
FacManageCarEntity fmce = new FacManageCarEntity();
fmce.SNumber = (req.page-1)*20+i+1;//序号赋值
int code;
if(fmcvs.get(i).isCarStatus()==false){
code = 1;
}else{
code = 2;
}
for(int j=0;j<dcs.size();j++){
if(dcs.get(j).getVal().equals(""+code)){
fmce.carStatusDesc = dcs.get(j).getItems();//车辆状态描述
break;
}
}
fmce.fmcv = fmcvs.get(i);
fmces.add(fmce);
}
pager.setRecordCount(count);
resp.pageCount = pager.getPageCount();
resp.count = count;
resp.fmces = fmces;
return resp;
}
@At
public Object fetchFacManageCar(FacManageCarReq req){
FacManageCarResp resp = new FacManageCarResp();
//车辆id
if(req.Id<=0){
resp.requestCode=-1;
return resp;
}
FacManageCarView fmcv = dao.fetch(FacManageCarView.class,Cnd.where("id","=",req.Id));
if(fmcv==null){
resp.requestCode=-1;
return resp;
}
FacManageCarEntity fmce = new FacManageCarEntity();
fmce.fmcv = fmcv;
int code;
if(fmcv.isCarStatus()==false){
code = 1;
}else{
code = 2;
}
Dictionary dc = dao.fetch(Dictionary.class,Cnd.where("type","=","car_state").and("val","=",code));
fmce.carStatusDesc = dc.getItems();//车辆使用状态描述
resp.requestCode=0;
resp.fmce = fmce;
return resp;
}
@At
public Object queryFacManageRecord(FacManageCarReq req){
FacManageCarResp resp = new FacManageCarResp();
Pager pager = dao.createPager(req.page, 20);
if(req.carId<=0){
resp.requestCode = -1;
return resp;
}
Cnd where = Cnd.where("carId","=",req.carId);
if (req.sortType == null) {
where.orderBy("createTime", "desc");
} else {
where.orderBy("convert (" + req.sortType.getName() + " using gbk)",
"" + req.sortType.getSorts() + "");
}
List<RepairRecord> rrs = dao.query(RepairRecord.class,where,pager);
if(rrs==null||rrs.size()==0){
resp.requestCode =-1;
return resp;
}
int count = dao.count(RepairRecord.class,where);
pager.setRecordCount(count);
List<FacManageRepairRecordEntity> fmrres = new ArrayList<FacManageRepairRecordEntity>();
List<Dictionary> dr = dao.query(Dictionary.class,Cnd.where("type","=","repairrecord_type"));
for(int i=0;i<rrs.size();i++){
FacManageRepairRecordEntity fmrre = new FacManageRepairRecordEntity();
fmrre.rr = rrs.get(i);
fmrre.SNmuber = (req.page-1)*20+i+1;
for(Dictionary d : dr){
if(d.getVal().equals(""+rrs.get(i).getType()+1)){
fmrre.typeDesc = d.getItems();//维修保养记录类型描述
}
}
fmrres.add(fmrre);
}
resp.requestCode=0;
resp.fmrres = fmrres;
resp.count = count;
resp.pageCount = pager.getPageCount();
return resp;
}
/**
* 维修保养项目接口
* @author fjd
* @param req
* @return
*/
@At
public Object queryFacManageOrderItme(FacManageOrderItmeReq req){
FacManageOrderItmeResp resp = new FacManageOrderItmeResp();
if(req.id<=0){
resp.requestCode = -1;
return resp;
}
List<OrderItme> ois = dao.query(OrderItme.class, Cnd.where("rrId","=",req.id));
if(ois==null||ois.size()==0){
resp.requestCode = -1;
return resp;
}
int count = dao.count(OrderItme.class,Cnd.where("rrId","=",req.id));
List<FacManageOrderItmeEntity> fmoies = new ArrayList<FacManageOrderItmeEntity>();
List<Dictionary> dlist = dao.query(Dictionary.class,Cnd.where("type","=","repairrecord_type"));
for(int i=0;i<ois.size();i++){
FacManageOrderItmeEntity fmoie = new FacManageOrderItmeEntity();
fmoie.oi = ois.get(i);
fmoie.SNumber = (req.page-1)*20+i+1;//序号
for(Dictionary d : dlist){
if(d.getVal().equals(""+ois.get(i).getType())){
fmoie.typeDesc = d.getItems();//维修保养记录描述
}
}
fmoies.add(fmoie);
}
resp.count = count;
resp.fmoies = fmoies;
return resp;
}
}
以上是关于返回json格式 不忽略null字段的主要内容,如果未能解决你的问题,请参考以下文章