简体   繁体   English

如何仅对Java对象或JSON负载中的某些字段执行特定操作

[英]How to do a specific operation to only certain fields in a Java object or a JSON payload

I have a spring boot microservices application and within it, I want to do a specific operation (say some string manip) on specific fields of a Java object (or a JSON object). 我有一个spring boot microservices应用程序,在其中,我想对Java对象(或JSON对象)的特定字段执行特定的操作(例如一些字符串操作)。

For example: 例如:

class Employee {

private String id;
private String name;
private String someOtherId;

}

If I need to do a specific operation only to id and someOtherId fields, how can go about it? 如果我只需要对id和someOtherId字段执行特定操作,该怎么办? Can there be custom annotations created to handle this? 是否可以创建自定义注释来处理此问题?

Something like: 就像是:

stringAppend(employee) should do this operation to only the specific fields. stringAppend(employee)应该仅对特定字段执行此操作。 I don't want to check iteratively inside the function, rather I would do it via configuration. 我不想在函数内部进行迭代检查,而是通过配置来进行检查。

The object will be a payload from a HTTPRequest and need to do this manipulation specifically to only certain fields. 该对象将是来自HTTPRequest的有效负载,并且需要专门针对特定字段进行此操作。

For manipulating the specific fields of received object in HTTP request payload, you can use Jackson library in Java. 要处理HTTP请求有效负载中接收到的对象的特定字段,可以使用Java中的Jackson库。 Jackson is a very popular and efficient java based library to serialize or map java objects to JSON and vice versa. Jackson是一个非常流行且高效的基于Java的库,用于将Java对象序列化或映射到JSON,反之亦然。

In this case from http request body, Employee object will be in serialized formed. 在这种情况下,从http请求正文开始,Employee对象将以序列化形式形成。 To deserialized it to actual Employee object you can use ObjectMapper from Jackson library as follows : 要将其反序列化为实际的Employee对象,可以使用Jackson库中的ObjectMapper,如下所示:

Assume payloadJson is a string which contains request payload in JSON format. 假设payloadJson是一个字符串,其中包含JSON格式的请求有效负载。

ObjectMapper objectMapper = new ObjectMapper();

Employee employee = objectMapper.readValue(payloadJson, Employee.class);

After desrialization you can perform manipulation on fields of Employee object using Getters and Setters method. 反序列化之后,您可以使用Getters和Setters方法对Employee对象的字段执行操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象序列化为json,仅某些字段 - Object serialization to json, certain fields only Java 只获取某些对象字段 - Java get only certain object fields 如何创建 JSON 文件,提取特定的 object 字段,基于 object 与 @JsonProperty 注释的所有字段 - ZD6C71E8DFF178D1 - How to create JSON file extracting specific object fields, basing on object with all fields annotated with @JsonProperty - JAVA 如何在 Z93F725A07423FE21C889F6D348B 的 JSON 数组中找到特定的 JSON object - How do you find a specific JSON object in a JSON array in java 如何根据JSON对象的字段值创建特定Java类的对象? - How to create an object of specific Java class from JSON object depending on it's fields values? 使用SpringBoot开发的REST API映射到Java Object时使JSON payload字段不区分大小写 - Make JSON payload fields case insensitive when mapping to Java Object in REST API developed using SpringBoot 如何使用 Java 仅查询 MongoDB 中的特定字段? - How to query only the specific fields in MongoDB with Java? 如果我在解析之前不知道所有 Z466DEEC76ECDF2456D38571F6Z 字段,如何将 JSON 转换为 Java object? - How to convert JSON to Java object if I do not know all json fields before parsing? 如何使用 retrofit 从 JSON 仅获取某些字段 - How to get only certain fields from JSON with retrofit 在带有Object参数的Java方法中,如何访问仅对特定类的实例存在的字段? - In a Java method that takes an Object argument, how can you access fields that only exist for instances of specific classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM