在java 6中,我尝试从服务获取数据。它有时返回布尔值,有时返回字符串,所以我为字符串创建了通用响应类,例如response:
{
"data": "D",
"success": true,
"error": null,
"validations": null
}
bollean:
{
"data": true,
"success": true,
"error": null,
"validations": null
}
响应类:
@XmlRootElement
public class BaseResponsefaj<T>{
private T data;
private boolean success = true;
private ExceptionInfo error;
private List<ValidationInfo> validations;
public BaseResponsefaj(T data){
this.data = data;
}
public BaseResponsefaj(T data, boolean success){
this.data = data;
this.success = success;
}
public BaseResponsefaj(){
}
public BaseResponsefaj(boolean success, ExceptionInfo error) {
super();
this.success = success;
this.error = error;
}
public BaseResponsefaj(boolean success, ExceptionInfo error, T data) {
super();
this.data = data;
this.success = success;
this.error = error;
}
public ExceptionInfo getError() {
return error;
}
public void setError(ExceptionInfo error) {
this.error = error;
}
public List<ValidationInfo> getValidations() {
return validations;
}
public void setValidations(List<ValidationInfo> validations) {
this.validations = validations;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "BaseResponse{" +
"data=" + data +
", success=" + success +
", error=" + error +
", validations=" + validations +
'}';
}
}
当尝试从service:
获取时得到错误。java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl incompatible with java.lang.String
and for also boolean.
我是这样调用的:
final BaseResponsefaj<?> response;
ClientResponse getResponse = webResource
.header("x-api-key", getAhtapotService().getapi())
.accept("application/json")
.type(MediaType.APPLICATION_JSON_TYPE)
.get(ClientResponse.class);
final int status = getResponse.getStatus();
if (HttpStatus.SC_OK == status) {
until here对于两个服务,字符串和布尔值相同。之后,我得到:
response = getResponse.getEntity(BaseResponsefaj.class);
在一个服务中转换为布尔值,在另一个字符串中:
(String) response.getData();
,但它给出错误。它是Jersey-bundle-1.19.1.jar .
我不想为每个类创建2个响应类。将来可能会有更多的类型。所以通用的会更好,但我不能解决这个问题。我不知道是否布尔响应类也可以修复
给出错误
"java.lang.ClassCastException:" org.apache.xerces.dom.ElementNSImpl"" incompatible" with java.lang.Boolean
,有时它给出
Class has two properties of the same name "data"
所以我做了这个
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class BaseResponseMfaj<T>{
@XmlElement(required=true)
private T data;
@XmlTransient
public T getData() {
return data;
}
但转换为布尔值继续
java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl incompatible with java.lang.Boolean
和
Object data = response.getData();
打印数据:[data: null]
String stringdata = String.valueOf(response.getData());
打印stringdata: [data: null]
我不明白为什么字符串或对象也是空的?
String booleandata = String.valueOf((Boolean) response.getData());
给出错误
不是很好的解决方案,但我确实喜欢这样。创建一个根类,从那里删除数据:
public class BaseParametricPrefixResponse {
private boolean success;
private ExceptionInfo error;
private List<ValidationInfo> validations;
}
和
BooleanParametricPrefixResponse extends BaseParametricPrefixResponse {
private boolean data;
public boolean isData() {
return data;
}
public void setData(boolean data) {
this.data = data;
}
对于bollean我创建了这个。我用这个打电话,很管用。对于string,我创建了另一个。对于列表字符串,我可能需要创建另一个。