Skip to content

Commit

Permalink
Merge pull request #7 from SelimHorri/develop
Browse files Browse the repository at this point in the history
map object from listener
  • Loading branch information
SelimHorri authored Aug 16, 2021
2 parents 150cd69 + 14ddcd9 commit 018ef80
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/selimhorri/app/config/JsonMapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.selimhorri.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

@Configuration
public class JsonMapperConfig {

@Bean
public ObjectMapper getJsonMapper() {
return new JsonMapper().configure(SerializationFeature.INDENT_OUTPUT, false);
}


}
18 changes: 16 additions & 2 deletions src/main/java/com/selimhorri/app/listener/KafkaListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.selimhorri.app.model.Message;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Component
@RequiredArgsConstructor
@Slf4j
public class KafkaListener {

private final ObjectMapper objectMapper;

@org.springframework.kafka.annotation.KafkaListener(topics = {"NewTopic"}, groupId = "group_id")
public void consume(final String msg) {
System.out.println(msg);
public void consume(final String messageString) throws JsonMappingException, JsonProcessingException {
final Message message = this.objectMapper.readValue(messageString.toString(), Message.class);
System.err.println(message);
log.info("mapped object printed! {}", message);
}


Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/selimhorri/app/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.selimhorri.app.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.NoArgsConstructor;

@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Message {

private final String msg;
private String msg;

}

0 comments on commit 018ef80

Please sign in to comment.