Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

泛型多重嵌套,导致不能正确解析 #569

@ding102992

Description

@ding102992
public class BaseResponse<T> {

    private Response<T> response;

    public Response<T> getResponse() {
        return response;
    }

    public void setResponse(Response<T> response) {
        this.response = response;
    }

    public static class Info{
        private int code;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }
    }

    public static class Response<T>{
        private T content;
        private Info info;

        public T getContent() {
            return content;
        }

        public void setContent(T content) {
            this.content = content;
        }

        public Info getInfo() {
            return info;
        }

        public void setInfo(Info info) {
            this.info = info;
        }
    }
}

将外层类类型形参传递给内层类之后,内层类无法正确解析,只能解析成为JSONObject。

JSON.parseObject(jsonStr,type);

通过以上的方式反序列化,type的类型为Type,非Class。

最终引用的时候,会报出异常:

com.alibaba.fastjson.JSONObject cannot be cast to com.ssqian.bestsign.sign.model.entities.login.LoginResponse$Body

LoginResponse的代码如下

public class LoginResponse extends BaseResponse<LoginResponse.Body>{

    public static class Body{
        private MemberInfo memberinfo;

        public MemberInfo getMemberinfo() {
            return memberinfo;
        }

        public void setMemberinfo(MemberInfo memberinfo) {
            this.memberinfo = memberinfo;
        }
    }

    public static class MemberInfo {
        private String name;
        private String email;
        /*
         省略Getter,Setter
        */
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions