【发布时间】:2019-05-15 09:51:53
【问题描述】:
我是 java 泛型的初学者,我正在尝试在我的类中使用泛型解析我的 user.yaml 文件。当我尝试解析 yaml 文件时,无法解析类型变量“T”不知道哪里出错了。
最初这只是一个普通的类,然后我实现了泛型。
my yaml file
user:
name: Test User
age: 30
public interface IUser {
String getName();
IUser setName(String name);
Integer getAge();
IUser setAge(Integer age);
}
public class User implements IUser{
@JsonProperty("name")
private String name;
@JsonProperty("age")
private Integer age;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public User setName(String name) {
this.name = name;
return this;
}
@JsonProperty("age")
public Integer getAge() {
return age;
}
@JsonProperty("age")
public User setAge(Integer age) {
this.age = age;
return this;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
return new ToStringBuilder(this).append("name", name).append("age", age).append("additionalProperties", additionalProperties).toString();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(additionalProperties).append(age).append(name).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof User) == false) {
return false;
}
User rhs = ((User) other);
return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).append(age, rhs.age).append(name, rhs.name).isEquals();
}
}
public interface IExample {
<T extends IUser> T getUser();
<T extends IUser> void setUser(T user);
}
public class Example implements IExample{
@JsonProperty("user")
private User user;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("user")
public <T extends IUser> T getUser() {
return (T) user;
}
@JsonProperty("user")
public <T extends IUser> void setUser(T t) {
this.user = (User)t;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
return new ToStringBuilder(this).append("user", user).append("additionalProperties", additionalProperties).toString();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(additionalProperties).append(user).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Example) == false) {
return false;
}
Example rhs = ((Example) other);
return new EqualsBuilder().append(additionalProperties, rhs.additionalProperties).append(user, rhs.user).isEquals();
}
}
my main class where i'm trying to parse yaml object
public class ReadYaml {
public void parse(){
String path = "path/to/user.yaml";
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
IExample example = mapper.readValue(new File(path), Example.class);
System.out.println(example.getUser().getName());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new ReadYaml().parse();
}
}
我希望输出是:Test User,但实际输出是 com.fasterxml.jackson.databind.JsonMappingException: Type variable 'T' can not be resolved (with context com.yaml.Example 类)
更新
updated yaml file
user:
name: Test User
age: 30
address:
line1: bangalore
line2: karnataka
public interface IAddress {
String getLine1();
IAddress setLine1(String line1);
String getLine2();
IAddress setLine2(String line2);
}
public class Address implements IAddress {
// code getters and setters
}
public interface IExample<T, U extends IUser & IAddress> {
T getUser();
void setUser(T user);
U getAddress();
void setAddress(U address);
}
public class Example<T, U extends IUser & IAddress> implements IExample<T, U>{
now i'm trying to parse yaml file.
public class ReadYaml {
.
.
.
TypeReference<Example<User, Address> typeReference = new TypeReference<Example<User, Address>() {
};
IExample example = mapper.readValue(new File(path), typeReference);
System.out.println(example.getAddress.getLine1());
我在声明 TypeReference<Example<User, Address> typeReference 时遇到错误:类型参数“地址”不在其范围内,应实施“IUser”
【问题讨论】:
标签: java generics compilation jackson yaml