platform-messaging При сериализации Json заголовки NULL полей не попадут в сообщение.
This commit is contained in:
parent
22c6ca754c
commit
42b7bca653
4 changed files with 166 additions and 0 deletions
|
|
@ -57,5 +57,12 @@
|
||||||
<groupId>org.springframework.kafka</groupId>
|
<groupId>org.springframework.kafka</groupId>
|
||||||
<artifactId>spring-kafka</artifactId>
|
<artifactId>spring-kafka</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- TEST -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package ru.spcex.clearing.platform.messaging.serialization;
|
package ru.spcex.clearing.platform.messaging.serialization;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.kafka.common.header.Headers;
|
import org.apache.kafka.common.header.Headers;
|
||||||
import org.apache.kafka.common.header.internals.RecordHeader;
|
import org.apache.kafka.common.header.internals.RecordHeader;
|
||||||
|
|
@ -14,6 +15,7 @@ public class JsonSerializer implements Serializer<Object> {
|
||||||
|
|
||||||
public JsonSerializer() {
|
public JsonSerializer() {
|
||||||
this.json = new ObjectMapper();
|
this.json = new ObjectMapper();
|
||||||
|
json.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
// this.typeMapper = new DefaultJackson2JavaTypeMapper();
|
// this.typeMapper = new DefaultJackson2JavaTypeMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
package ru.spcex.clearing.platform.messaging.serialization;
|
||||||
|
|
||||||
|
import org.apache.kafka.common.header.Headers;
|
||||||
|
import org.apache.kafka.common.header.internals.RecordHeaders;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
||||||
|
import ru.spcex.clearing.platform.messaging.domain.BaseRequest;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see JsonSerializerTest
|
||||||
|
*/
|
||||||
|
class JsonDeserializerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDeserialize() throws UnsupportedEncodingException {
|
||||||
|
Headers headers;
|
||||||
|
byte[] jsonB;
|
||||||
|
{
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
|
||||||
|
JsonSerializerTest.TestObj data = new JsonSerializerTest.TestObj();
|
||||||
|
data.setCompanyId(123L);
|
||||||
|
data.setSection("Section \\<>?'\" 2 Секция два.");
|
||||||
|
|
||||||
|
BaseRequest<JsonSerializerTest.TestObj> req = new BaseRequest<>();
|
||||||
|
req.setId(12L);
|
||||||
|
req.setActionType(ActionType.NEW);
|
||||||
|
req.setUserId(1020L);
|
||||||
|
req.setRequestPayload(data);
|
||||||
|
headers = new RecordHeaders();
|
||||||
|
jsonB = serializer.serialize("topic-1", headers, req);
|
||||||
|
String json = new String(jsonB, "utf-8");
|
||||||
|
assertEquals("{\"id\":12,\"actionType\":\"NEW\",\"requestPayload\":{\"companyId\":123,\"section\":\"Section \\\\<>?'\\\" 2 Секция два.\"},\"userId\":1020}",
|
||||||
|
json);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
JsonDeserializer deserializer = new JsonDeserializer();
|
||||||
|
|
||||||
|
Object o = deserializer.deserialize("topic-1", headers, jsonB);
|
||||||
|
BaseRequest<JsonSerializerTest.TestObj> req = (BaseRequest<JsonSerializerTest.TestObj>) o;
|
||||||
|
assertEquals(ActionType.NEW, req.getActionType());
|
||||||
|
assertEquals(12L, req.getId());
|
||||||
|
assertEquals("Section \\<>?'\" 2 Секция два.", req.getRequestPayload().getSection());
|
||||||
|
assertEquals(123L, req.getRequestPayload().getCompanyId());
|
||||||
|
assertNull(req.getRequestPayload().getTaskName());
|
||||||
|
assertNull(req.getRequestPayload().getUserId());
|
||||||
|
assertEquals(0, req.getUnknownProperties().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
package ru.spcex.clearing.platform.messaging.serialization;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import org.apache.kafka.common.header.Headers;
|
||||||
|
import org.apache.kafka.common.header.internals.RecordHeaders;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import ru.spcex.clearing.platform.messaging.domain.ActionType;
|
||||||
|
import ru.spcex.clearing.platform.messaging.domain.BaseRequest;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class JsonSerializerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void serialize() throws UnsupportedEncodingException {
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
|
||||||
|
TestObj data = new TestObj();
|
||||||
|
data.setCompanyId(123L);
|
||||||
|
data.setSection("Section \\<>?'\" 2 Секция два.");
|
||||||
|
|
||||||
|
byte[] jsonB = serializer.serialize("topic-1", data);
|
||||||
|
String json = new String(jsonB, "utf-8");
|
||||||
|
assertEquals("{\"companyId\":123,\"section\":\"Section \\\\<>?'\\\" 2 Секция два.\"}",
|
||||||
|
json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void serialize2() throws UnsupportedEncodingException {
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
|
||||||
|
TestObj data = new TestObj();
|
||||||
|
data.setCompanyId(123L);
|
||||||
|
data.setSection("Section \\<>?'\" 2 Секция два.");
|
||||||
|
|
||||||
|
BaseRequest<TestObj> req = new BaseRequest<>();
|
||||||
|
req.setId(12L);
|
||||||
|
req.setActionType(ActionType.NEW);
|
||||||
|
req.setUserId(1020L);
|
||||||
|
req.setRequestPayload(data);
|
||||||
|
Headers headers = new RecordHeaders();
|
||||||
|
byte[] jsonB = serializer.serialize("topic-1", headers, req);
|
||||||
|
String json = new String(jsonB, "utf-8");
|
||||||
|
assertEquals("{\"id\":12,\"actionType\":\"NEW\",\"requestPayload\":{\"companyId\":123,\"section\":\"Section \\\\<>?'\\\" 2 Секция два.\"},\"userId\":1020}",
|
||||||
|
json);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TestObj {
|
||||||
|
@JsonProperty
|
||||||
|
private Long userId;
|
||||||
|
@JsonProperty
|
||||||
|
private String taskName;
|
||||||
|
@JsonProperty
|
||||||
|
private Long companyId;
|
||||||
|
@JsonProperty
|
||||||
|
private Long securityId;
|
||||||
|
@JsonProperty
|
||||||
|
private String section;
|
||||||
|
|
||||||
|
public void setUserId(Long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTaskName(String taskName) {
|
||||||
|
this.taskName = taskName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompanyId(Long companyId) {
|
||||||
|
this.companyId = companyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecurityId(Long securityId) {
|
||||||
|
this.securityId = securityId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSection(String section) {
|
||||||
|
this.section = section;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTaskName() {
|
||||||
|
return taskName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCompanyId() {
|
||||||
|
return companyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getSecurityId() {
|
||||||
|
return securityId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSection() {
|
||||||
|
return section;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue