复制代码

为懒人提供无限可能,生命不息,code不止

人类感性的情绪,让我们知难行难
我思故我在
日拱一卒,功不唐捐
  • 首页
  • 前端
  • 后台
  • 数据库
  • 运维
  • 资源下载
  • 实用工具
  • 接口文档工具
  • 登录
  • 注册

springboot

【原创】SerializationFeature.FAIL_ON_EMPTY_BEANS 应用及简介

作者: whooyun发表于: 2018-04-14 14:51

平时json转换都用的是google的Gson,框架换成springboot后发现出冲突了,用Gson封装的报文不能用了,所以整理下。

我的Gson报文封装类

public class ResponseMsgBean {

    private static final String RESULT = "result";
    private static Gson gson = new Gson();

    /**
     * 错误码
     */
    private String resultCode;

    /**
     * 错误信息
     */
    private String resultMsg;

    /**
     * 业务数据
     */
    private Object body;

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public static void main(String[] args) {

         System.out.println(ResponseMsgBean.getMsgJson("7001", "7001"));
    }

    public ResponseMsgBean(String resultCode, String resultMsg, Object body) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        if (StringUtils.isEmpty(body)) {
            this.body = new Object();
        } else {
            this.body = body;
        }
    }

    public ResponseMsgBean(String resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.body = new Object();
    }

    /**
     * 自定义信息code、信息msg、业务数据
     *
     * @param resultCode 消息代码
     * @param resultMsg  消息内容
     * @param body       业务数据(可选项,为空null)
     * @return
     */
    public static String getMsgJson(@NonNull String resultCode, @NonNull String resultMsg, Object body) {
        ResponseMsgBean msgBean = new ResponseMsgBean(resultCode, resultMsg, body);
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }


    /**
     * 自定义信息code、信息msg
     * @param resultCode 消息代码
     * @param resultMsg  消息内容
     * @return
     */
    public static String getMsgJson(@NonNull String resultCode, @NonNull String resultMsg) {
        ResponseMsgBean msgBean = new ResponseMsgBean(resultCode, resultMsg);
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }

    /**
     * 请求参数或格式错误
     *
     * @return
     */
    public static String getMsgJsonParamError() {
        ResponseMsgBean msgBean = new ResponseMsgBean(ResultCode.PARAMS_ERROR.getCode(), ResultCode.PARAMS_ERROR.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }

    /**
     * 操作成功返回业务数据
     *
     * @param body 业务数据
     * @return
     */
    public static String getMsgJsonWithData(@NonNull Object body) {
        ResponseMsgBean msgBean = new ResponseMsgBean(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), body);
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }

    /**
     * 系统异常
     *
     * @return
     */
    public static String getMsgJsonWithSystemError() {
        ResponseMsgBean msgBean = new ResponseMsgBean(ResultCode.FB_SYSTEM_ERROR.getCode(), ResultCode.FB_SYSTEM_ERROR.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }

    /**
     * 操作成功(insert,update,delete)
     *
     * @return
     */
    public static String getMsgJsonWithSuccess() {
        ResponseMsgBean msgBean = new ResponseMsgBean(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }

    /**
     * 查询成功未返回业务数据
     *
     * @return
     */
    public static String getMsgJsonWithoutData() {
        ResponseMsgBean msgBean = new ResponseMsgBean(ResultCode.NODATA.getCode(), ResultCode.NODATA.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return gson.toJson(map);
    }

}

我的json转换类

@Slf4j
public class WrapperJson {


    ObjectMapper mapper;

    public  WrapperJson()
    {
        mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        /**
         * 如果被转换对象是Object会提示
         * Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
         */
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }

    public  WrapperJson(JsonInclude.Include include)
    {
        mapper = new ObjectMapper();
        mapper.setSerializationInclusion(include);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        /**
         * 如果被转换对象是Object会提示
         * Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
         */
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    }


    public  String toJson(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }

    public  <T> T fromJson(String json, Class<T> cls) throws IOException {
        return mapper.readValue(json, cls);
    }

    public  <T> T fromJson(String json,TypeReference valueTypeRef) throws IOException {
        return mapper.readValue(json, valueTypeRef);
    }

}
转到jackson出现的异常

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

问题原因:
业务数据body是Object类型,其实有时候就是个空对象,所以是无法序列化,jackson 默认是对所有数据类型进行序列化。所以就只能自定义ObjectMapper,配置mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)

注:看到网上有可以针对某个字段不做序列化的注解配置@JsonIgnore,但是不适合我的这种情况