SUPPORT-9422: add deserializer

This commit is contained in:
adel.ka 2025-09-22 17:13:14 +03:00
parent d77b7274a7
commit e52cd3138b
5 changed files with 77 additions and 9 deletions

View file

@ -3,6 +3,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.time.Duration; import java.time.Duration;
import java.util.Map;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
@ -11,6 +12,7 @@ import javax.sql.DataSource;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.StreamReadConstraints; import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
@ -40,7 +42,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import ru.micord.ervu.account_applications.deserializer.DataWrapperDeserializer;
import ru.micord.ervu.account_applications.deserializer.ReferenceEntityDeserializer; import ru.micord.ervu.account_applications.deserializer.ReferenceEntityDeserializer;
import ru.micord.ervu.account_applications.model.DataWrapper;
import ru.micord.ervu.account_applications.model.ReferenceEntity; import ru.micord.ervu.account_applications.model.ReferenceEntity;
/** /**
@ -162,7 +166,7 @@ public class AppConfig {
.build(); .build();
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
module.addDeserializer(ReferenceEntity.class, new ReferenceEntityDeserializer()); customDeserializers().forEach(module::addDeserializer);
return new ObjectMapper(factory) return new ObjectMapper(factory)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
@ -171,4 +175,11 @@ public class AppConfig {
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true) .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
.registerModules(new JavaTimeModule(), module); .registerModules(new JavaTimeModule(), module);
} }
private Map<Class, JsonDeserializer> customDeserializers() {
return Map.of(
ReferenceEntity.class, new ReferenceEntityDeserializer(),
DataWrapper.class, new DataWrapperDeserializer()
);
}
} }

View file

@ -0,0 +1,39 @@
package ru.micord.ervu.account_applications.deserializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import ru.micord.ervu.account_applications.model.DataWrapper;
/**
* @author Adel Kalimullin
*/
public class DataWrapperDeserializer extends JsonDeserializer<DataWrapper> {
@Override
public DataWrapper deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException, JacksonException {
JsonNode node = jsonParser.readValueAsTree();
if (node.isTextual()){
return new DataWrapper(List.of(node.asText()));
}
else if (node.isArray()){
List<String> values = new ArrayList<>();
for (JsonNode element : node) {
if (element.isTextual()) {
values.add(element.asText());
}
}
return new DataWrapper(values);
}
return new DataWrapper(Collections.emptyList());
}
}

View file

@ -1,9 +1,7 @@
package ru.micord.ervu.account_applications.kafka.model; package ru.micord.ervu.account_applications.kafka.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import ru.micord.ervu.account_applications.model.ReferenceEntity; import ru.micord.ervu.account_applications.model.DataWrapper;
/** /**
* @author Adel Kalimullin * @author Adel Kalimullin
@ -11,13 +9,13 @@ import ru.micord.ervu.account_applications.model.ReferenceEntity;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class ChangeActiveMessage { public class ChangeActiveMessage {
private boolean success; private boolean success;
private List<String> data; private DataWrapper data;
public List<String> getData() { public DataWrapper getData() {
return data; return data;
} }
public void setData(List<String> data) { public void setData(DataWrapper data) {
this.data = data; this.data = data;
} }

View file

@ -0,0 +1,18 @@
package ru.micord.ervu.account_applications.model;
import java.util.List;
/**
* @author Adel Kalimullin
*/
public class DataWrapper {
private final List<String> ids;
public DataWrapper(List<String> values) {
this.ids = values;
}
public List<String> getIds() {
return ids;
}
}

View file

@ -99,13 +99,15 @@ public class ErvuDirectoriesService {
ChangeActiveMessage.class ChangeActiveMessage.class
); );
if (changeActiveMessage.isSuccess() && !CollectionUtils.isEmpty(changeActiveMessage.getData())) { if (changeActiveMessage.isSuccess() &&
changeActiveMessage.getData() != null &&
!CollectionUtils.isEmpty(changeActiveMessage.getData().getIds())) {
DataProcessor<T, ?> processor = (DataProcessor<T, ?>) dataProcessors.get(entityClass); DataProcessor<T, ?> processor = (DataProcessor<T, ?>) dataProcessors.get(entityClass);
if (processor == null) { if (processor == null) {
throw new IllegalStateException("No processor found for " + entityClass.getSimpleName()); throw new IllegalStateException("No processor found for " + entityClass.getSimpleName());
} }
processor.changeActiveStatus(changeActiveMessage.getData(), active); processor.changeActiveStatus(changeActiveMessage.getData().getIds(), active);
} }
} }
catch (Exception e) { catch (Exception e) {