

I need to print the request body with masked data in the console. So I want to separate the request object that is masked for printing to the console from the request object that is used for business without masking.
So I cloned the request object (underwriterequest request) to a new object (underwriterequest requestmasking) and then used the method to mask the data.
But when I set the shielding ID to new object, the reference object (underwriterrequest request) also has shielding.
I don't want to block (underwriterequest request) because the object must be used in business logic.
Why does it affect the reference object? How can I mask data without affecting the reference object?
Thanks.
public @ResponseBody ResponseEntity<UnderwriteResponse> execute(@RequestBody UnderwriteRequest request) { UnderwriteRequest requestMasking = maskingData(request); } public static UnderwriteRequest maskingData(UnderwriteRequest request) throws CloneNotSupportedException { UnderwriteRequest requestMasking = (UnderwriteRequest) request.clone(); try { if(request != null) { if(request.getRequestBody().getPersonalData().getIdCard() != null && !request.getRequestBody().getPersonalData().getIdCard().isEmpty()) { maskIdCard(requestMasking, request); } }catch (Exception e) { log.info(e); } return requestMasking; } public static void maskIdCard(UnderwriteRequest requestMasking, UnderwriteRequest request) { String maskChar = "X"; String maskString = StringUtils.repeat( maskChar, 9); String idcard = request.getRequestBody().getPersonalData().getIdCard(); requestMasking.getRequestBody().getPersonalData().setIdCard(StringUtils.overlay(idcard, maskString, 0, 9)); } @Getter @Setter public class UnderwriteRequest implements Cloneable{ private RequestHeader requestHeader; private RequestBrmsBody requestBrmsBody; private RequestBody requestBody; private RESPONSE_STATUS status = RESPONSE_STATUS.FAILED; private String message; @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } }
I use the clone() method to copy the object value to a new object. Because I think when I change the data in the new object, it won't affect the reference object. https://www.geeksforgeeks.org/clone-method-in-java-2/
Correct Answer
It seems that you are using SpringBoot framework. You can take a look at the BeanUtils.copyProperties(); method. This method assigns the same properties to the first and second objects via reflection. There won’t be the problem you mentioned
The above is the detailed content of In Java, how to copy an object value to a new object and change the data without affecting another reference variable. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment