.net json 属性 中的转换 java - @JsonProperty
.net json property conversion in java - @JsonProperty
这里需要一些帮助!我有一个 Java Rest API,它从 .net 端点获取数据并将其传递给 UI。 JSON 属性是大写的,我想在将它们发送到 UI 之前将它们转换为 JAVA。对此有任何指示吗?
在 java 中,我有一个 class 如下所示:
public class Person {
@JsonProperty("Name")
private String name;
@JsonProperty("Age")
private int age;
}
我正在使用 @JsonProperty,因为 .net 中的键以大写字母开头。在将它发送到 Java 中的 UI 之前,如何将其转换回去?
感谢您的帮助!
创建另一个具有相同结构的 class 并使用您想要的其他名称。像这样:
// Class to read .NET object
public class Person {
@JsonProperty("Name")
private String name;
@JsonProperty("Age")
private int age;
}
// Class to represent the object in Java REST API
public class Person {
@JsonProperty("name")
private String name;
@JsonProperty("age")
private int age;
}
// Class to represent the object in Java REST API,
// in case you use some standard library that
// uses property names for JSON as is
public class Person {
private String name;
private int age;
}
当然你应该把这些class放在不同的包里。
您的代码如下所示:
xxx.dotnet.Person dotnetPerson = doSomethingViaDotNet(...);
yyy.rest.Person restPerson = new yyy.rest.Person();
restPerson.setName(dotnetPerson.getName());
restPerson.setAge(dotnetPerson.getAge());
...
return restPerson;
如果您决定使用 MapStruct,您的代码可能如下所示:
@Mapper
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );
yyy.rest.Person dotnetToRest(xxx.dotnet.Person dotnetPerson);
}
由于所有属性都具有相同的名称和类型,因此您的映射器中不需要任何其他内容。
MapStruct 将生成一个实现此接口的 class。用法如下:
restPerson = PersonMapper.INSTANCE.dotnetToRest(dotnetPerson);
这里需要一些帮助!我有一个 Java Rest API,它从 .net 端点获取数据并将其传递给 UI。 JSON 属性是大写的,我想在将它们发送到 UI 之前将它们转换为 JAVA。对此有任何指示吗?
在 java 中,我有一个 class 如下所示:
public class Person {
@JsonProperty("Name")
private String name;
@JsonProperty("Age")
private int age;
}
我正在使用 @JsonProperty,因为 .net 中的键以大写字母开头。在将它发送到 Java 中的 UI 之前,如何将其转换回去?
感谢您的帮助!
创建另一个具有相同结构的 class 并使用您想要的其他名称。像这样:
// Class to read .NET object
public class Person {
@JsonProperty("Name")
private String name;
@JsonProperty("Age")
private int age;
}
// Class to represent the object in Java REST API
public class Person {
@JsonProperty("name")
private String name;
@JsonProperty("age")
private int age;
}
// Class to represent the object in Java REST API,
// in case you use some standard library that
// uses property names for JSON as is
public class Person {
private String name;
private int age;
}
当然你应该把这些class放在不同的包里。
您的代码如下所示:
xxx.dotnet.Person dotnetPerson = doSomethingViaDotNet(...);
yyy.rest.Person restPerson = new yyy.rest.Person();
restPerson.setName(dotnetPerson.getName());
restPerson.setAge(dotnetPerson.getAge());
...
return restPerson;
如果您决定使用 MapStruct,您的代码可能如下所示:
@Mapper
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );
yyy.rest.Person dotnetToRest(xxx.dotnet.Person dotnetPerson);
}
由于所有属性都具有相同的名称和类型,因此您的映射器中不需要任何其他内容。
MapStruct 将生成一个实现此接口的 class。用法如下:
restPerson = PersonMapper.INSTANCE.dotnetToRest(dotnetPerson);