java接口作为jsonproperty字段

58wvjzkj  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(223)

我有一个响应对象,它总是由两个始终存在的响应属性和一个对象组成,目前,一个对象有响应特定的值,这不是很好,因为我必须有不同类型的响应,以便将值Map到不同的接口实现,这样我就可以检查值在特定的响应属性中是否不是null或null。
我的想法是使用接口类而不是operationresponse,并且基于调用有不同的实现,但是我有点不知道如何做到这一点,非常感谢任何帮助,谢谢大家

@JsonIgnoreProperties(ignoreUnknown = true)
public class NowOSResponse {

    @JsonProperty
    private String operation = "";
    @JsonProperty
    private String version = "";
    @JsonProperty(required = true)
    private OperationResponse response;
    @JsonProperty
    private String status;
}

这是主响应中的响应属性对象,当前它具有前面提到的特定值:

@JsonIgnoreProperties(ignoreUnknown = true)
public class OperationResponse {

    @JsonProperty
    private String accountID = "";
    @JsonProperty
    private String authToken = "";
    @JsonProperty
    private String language = "";
}

所以我所有的api调用都会返回一个响应,当然,目前我只Mapcreate account response object values accountid authtoken language,但是如何根据不同的调用返回不同的响应呢。

@RequiredArgsConstructor
public class AccountApi extends BaseApi {

    private final Credentials credentials;

    public NowOSResponse authorizeAccount(AccountAuth accountAuth) {
        return call(accountAuth.getRequestUrlWithParams());
    }

    public NowOSResponse createAccount() {
        NowOSResponse nowOSResponse = authorizeAccount(new AccountAuth());
        return call(new AccountStore(nowOSResponse.getResponse().getAccountID(),
                nowOSResponse.getResponse().getAuthToken(), credentials.getEmail(), credentials.getPassword()).getRequestUrlWithParams());
    }
}

下面是我放在baseapi类中的一个方法,以避免代码重复

public NowOSResponse call(String urlWithParam) {
        try {
            return nowOS.callNowOSOperation(RequestBody.create(urlWithParam, DEFAULT_MEDIA_TYPE)).execute().body();
        } catch (IOException e) {
            throw new RuntimeException("Could not execute the call" + e);
        }
    }

我就是这么看的

public interface NowResponse<T extends BaseResponse>  {

    T response();

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题