背景:接口返回基类有三个字段,但是其中有个字段包含敏感信息,接口转换成JSON不应该将这个字段返回,但是该字段应用于记录日志等。
解决方案:通过注解@JsonView解决
基类
package com.iflytek.kuyin.service.common.entity.api.base;
import com.fasterxml.jackson.annotation.JsonView;
import com.iflytek.kuyin.apidocs.build.annotation.FieldDocs;
import com.iflytek.kuyin.service.common.entity.annotation.JsonFieldNotEmpty;
import com.iflytek.kuyin.service.common.utils.config.ReturnCodeUtil;
import java.io.Serializable;
/**
* 返回基础类
*
* @author llfang2
*/
public class ApiBaseResponse implements Serializable, JsonFieldNotEmpty {
private static final long serialVersionUID = 3529070432097840540L;
/**
* 返回码
*/
@FieldDocs(desc = "返回码")
private String retcode;
/**
* 返回描述(展现于用户)
*/
@FieldDocs(desc = "返回描述")
private String retdesc;
/**
* 描述(系统内部描述)
*/
private transient String desc;
private String tc;
public String getTc() {
return tc;
}
public void setTc(String tc) {
this.tc = tc;
}
public String getRetcode() {
return retcode;
}
public void setRetcode(String retcode) {
this.retcode = retcode;
}
@JsonView(WithoutDescView.class)
public String getRetdesc() {
return retdesc;
}
public void setRetdesc(String retdesc) {
this.retdesc = retdesc;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public ApiBaseResponse() {
super();
this.retcode = ApiCommonRetCode.SUCCESS;
this.retdesc = ReturnCodeUtil.getReturnDescription(retcode);
}
public ApiBaseResponse(String retcode) {
super();
this.retcode = retcode;
this.retdesc = ReturnCodeUtil.getReturnDescription(retcode);
this.desc = retdesc;
}
public ApiBaseResponse(String retcode, String desc) {
super();
this.retcode = retcode;
this.desc = desc;
this.retdesc = ReturnCodeUtil.getReturnDescription(retcode);
}
public ApiBaseResponse(String retcode, String desc, String tc) {
this(retcode, desc);
this.tc = tc;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ApiBaseResponse{");
sb.append("retcode='").append(retcode).append('\'');
sb.append(", retdesc='").append(retdesc).append('\'');
sb.append(", desc='").append(desc).append('\'');
sb.append(", tc='").append(tc).append('\'');
sb.append('}');
return sb.toString();
}
}
其中内部描述需要在接口返回前端时屏蔽
修改后:
package com.iflytek.kuyin.service.common.entity.api.base;
import com.fasterxml.jackson.annotation.JsonView;
import com.iflytek.kuyin.apidocs.build.annotation.FieldDocs;
import com.iflytek.kuyin.service.common.entity.annotation.JsonFieldNotEmpty;
import com.iflytek.kuyin.service.common.utils.config.ReturnCodeUtil;
import java.io.Serializable;
/**
* 返回基础类
*
* @author llfang2
*/
public class ApiBaseResponse implements Serializable, JsonFieldNotEmpty {
private static final long serialVersionUID = 3529070432097840540L;
public interface WithoutDescView {
}
;
/**
* 返回码
*/
@FieldDocs(desc = "返回码")
private String retcode;
/**
* 返回描述(展现于用户)
*/
@FieldDocs(desc = "返回描述")
private String retdesc;
/**
* 描述(系统内部描述)
*/
private transient String desc;
private String tc;
@JsonView(WithoutDescView.class)
public String getTc() {
return tc;
}
public void setTc(String tc) {
this.tc = tc;
}
@JsonView(WithoutDescView.class)
public String getRetcode() {
return retcode;
}
public void setRetcode(String retcode) {
this.retcode = retcode;
}
@JsonView(WithoutDescView.class)
public String getRetdesc() {
return retdesc;
}
public void setRetdesc(String retdesc) {
this.retdesc = retdesc;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public ApiBaseResponse() {
super();
this.retcode = ApiCommonRetCode.SUCCESS;
this.retdesc = ReturnCodeUtil.getReturnDescription(retcode);
}
public ApiBaseResponse(String retcode) {
super();
this.retcode = retcode;
this.retdesc = ReturnCodeUtil.getReturnDescription(retcode);
this.desc = retdesc;
}
public ApiBaseResponse(String retcode, String desc) {
super();
this.retcode = retcode;
this.desc = desc;
this.retdesc = ReturnCodeUtil.getReturnDescription(retcode);
}
public ApiBaseResponse(String retcode, String desc, String tc) {
this(retcode, desc);
this.tc = tc;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ApiBaseResponse{");
sb.append("retcode='").append(retcode).append('\'');
sb.append(", retdesc='").append(retdesc).append('\'');
sb.append(", desc='").append(desc).append('\'');
sb.append(", tc='").append(tc).append('\'');
sb.append('}');
return sb.toString();
}
}
使用时仅需要带上注解@JsonView(ApiBaseResponse.WithoutDescView.class)即可不返回对应字段
比如:
/**
* 用户开通爱音乐会员
*
* @param request
* @param bindingResult
* @return
*/
@MethodName("用户开通爱音乐会员")
@RequestMapping(value = "m_s_c_tim", method = RequestMethod.GET)
public
@ResponseBody
@JsonView(ApiBaseResponse.WithoutDescView.class)
SetIMusicVipResponse setIMusicVip(@Valid SetIMusicVipRequest request, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return new SetIMusicVipResponse(ReturnCodeInterface.PARAM_INVALID_ERROR, bindingResult.getFieldError().getDefaultMessage());
}
return setIMusicVipResponse.setIMusicVipResponse(request);
}
使用postman前后结果对比