【Java】03 Java 解决类型擦除

1、测试解决代码

package com.hao.demo.exception;

import com.google.gson.*;
import com.hao.demo.common.domain.base.ResultInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haojunhu
 * @date 2020-07-23
 */
public class GenericDemo {

    public static void main(String[] args) throws Exception {
        /**
         * 产生json 串
         */
        //generateJsonString1();
        //generateJsonString2();
        /**
         * 解决数组问题
         */
        String body1 ="{\n" +
                "  \"success\": false,\n" +
                "  \"ret\": 1,\n" +
                "  \"code\": \"code\",\n" +
                "  \"msg\": \"msg\",\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"name\": \"lisi\",\n" +
                "      \"age\": \"hhh\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"lisi\",\n" +
                "      \"age\": \"hfah\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"lifasi\",\n" +
                "      \"age\": \"hhfah\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        String data1 = new Gson().fromJson(body1, ResultInfo.class).getData().toString();
        List<StudyResponse> responses= fromJsonArray(data1,StudyResponse.class);
        System.out.println(responses.get(0));

        /**
         * 可以解决对象 、对象中加数组
         */
        String body2 ="{\n" +
                "  \"success\": false,\n" +
                "  \"ret\": 1,\n" +
                "  \"code\": \"code\",\n" +
                "  \"msg\": \"msg\",\n" +
                "  \"data\": {\n" +
                "    \"name\": \"lifasi\",\n" +
                "    \"age\": \"hhfah\"\n" +
                "  }\n" +
                "}";

        String data2 = new Gson().fromJson(body2, ResultInfo.class).getData().toString();
        StudyResponse response= fromJsonObject(data2,StudyResponse.class);
        System.out.println(response);

    }


    public static void generateJsonString1() {
        Result<List<StudyResponse>> result = new Result<>();
        List<StudyResponse> responses = new ArrayList<>();
        responses.add(StudyResponse.builder().age("hhh").name("lisi").build());
        responses.add(StudyResponse.builder().age("hfah").name("lisi").build());
        responses.add(StudyResponse.builder().age("hhfah").name("lifasi").build());
        result.withData(responses);
        System.out.println(ObjectToJSONString(result));
    }

    public static void generateJsonString2() {
        Result<StudyResponse> result = new Result<>();
        result.withData(StudyResponse.builder().age("hhfah").name("lifasi").build());
        System.out.println(ObjectToJSONString(result));
    }


    /**
     * 解决办法
     */

    public static String ObjectToJSONString(Object obj) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        return gson.toJson(obj);
    }

    public static <T> List<T> fromJsonArray(String json, Class<T> clazz) throws Exception {
        List<T> lst = new ArrayList<T>();

        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            lst.add(new Gson().fromJson(elem, clazz));
        }

        return lst;
    }

    public static <T> T fromJsonObject(String json, Class<T> clazz) throws Exception {
        JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
        return new Gson().fromJson(jsonObject, clazz);
    }
}

2、封装信息类

package com.hao.demo.exception;

import java.io.Serializable;

/**
 * @author
 * @date 2020-07-12
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 68322011342765348L; // 序列化时用
    private boolean success = false;
    private int ret;
    private String code;
    private String msg;
    private T data;

    public Result() {
        this.ret = MyExceptionEnum.BASE_ERROR.getRet();
        this.code = MyExceptionEnum.BASE_ERROR.getCode();
        this.msg = MyExceptionEnum.BASE_ERROR.getMsg();
        this.data = null;
    }

    public Result<T> succeed() {
        return this.succeed(null);
    }

    public Result<T> succeed(T data) {
        return this.succeed(data, "SUCCESS", "success");
    }

    public Result<T> succeed(T data, String code, String msg) {
        this.success = true;
        this.ret = 0;
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    public Result<T> withSuccess(boolean success) {
        this.success = success;
        if (success) {
            this.ret = 0;
        }

        return this;
    }

    public Result<T> withRet(int ret) {
        this.ret = ret;
        return this;
    }

    public Result<T> withCode(String code) {
        this.code = code;
        return this;
    }

    public Result<T> withMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> withData(T data) {
        this.data = data;
        return this;
    }

    public Result<T> fail(MyExceptionEnum e) {
        this.success = false;
        this.ret = e.getRet();
        this.code = e.getCode();
        this.msg = e.getMsg();
        return this;
    }

    public Result<T> fail(MyExceptionEnumInterface exception) {
        this.success = false;
        this.ret = exception.getRet();
        this.code = exception.getCode();
        this.msg = exception.getMsg();
        return this;
    }

    public Result<T> fail(Result<T> other) {
        this.success = false;
        this.ret = other.ret;
        this.code = other.code;
        this.msg = other.msg;
        this.data = other.getData();
        return this;
    }

    public Result<T> fail(int ret, String code, String msg) {
        this.success = false;
        this.ret = ret;
        this.code = code;
        this.msg = msg;
        return this;
    }

    public static long getSerialversionuid() {
        return 4886206811288786668L;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public int getRet() {
        return this.ret;
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public String toString() {
        return "ResultInfo [success=" + this.success + ", ret=" + this.ret + ", code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + "]";
    }
}

3、枚举类

package com.hao.demo.exception;

/**
 * @author haojunhu
 * @date 2020-06-29
 */
public enum ResultEnum {

    // 200 操作成功 500 操作失败
    SUCCESS("200", "操作成功"),
    FAIL("500", "操作失败"),
    RPC_ERROR("500", "远程调用失败"),
    INVALID_REQUEST_PARAM_ERROR("500", "参数校验错误"),
    ;

    private String code;

    private String msg;

    ResultEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

你可能感兴趣的:(【java】基础知识点涵盖)