Jackson serialization test
This commit is contained in:
@ -1,10 +1,13 @@
|
|||||||
package mops.gruppen2.controller;
|
package mops.gruppen2.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.github.javafaker.Faker;
|
import com.github.javafaker.Faker;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
import mops.gruppen2.domain.ProductSwaggerExample;
|
import mops.gruppen2.domain.ProductSwaggerExample;
|
||||||
|
import mops.gruppen2.domain.event.AddUserEvent;
|
||||||
|
import mops.gruppen2.service.SerializationService;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -19,6 +22,11 @@ public class SwaggerAPIControllerExample {
|
|||||||
|
|
||||||
private final Faker faker = new Faker();
|
private final Faker faker = new Faker();
|
||||||
private final List<ProductSwaggerExample> products = new ArrayList<>();
|
private final List<ProductSwaggerExample> products = new ArrayList<>();
|
||||||
|
private final SerializationService serializationService;
|
||||||
|
|
||||||
|
public SwaggerAPIControllerExample(SerializationService serializationService) {
|
||||||
|
this.serializationService = serializationService;
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/get/all")
|
@GetMapping("/get/all")
|
||||||
@ApiOperation(value = "Erzeugt eine Liste mit allen gespeicherten Produkten")
|
@ApiOperation(value = "Erzeugt eine Liste mit allen gespeicherten Produkten")
|
||||||
@ -44,4 +52,19 @@ public class SwaggerAPIControllerExample {
|
|||||||
|
|
||||||
return "Product saved successfully";
|
return "Product saved successfully";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/json")
|
||||||
|
public void json() {
|
||||||
|
try {
|
||||||
|
serializationService.serializeEvent(new AddUserEvent(
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
"Eins",
|
||||||
|
faker.leagueOfLegends().location(),
|
||||||
|
faker.name().lastName(),
|
||||||
|
"123@email.de"));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
package mops.gruppen2.domain.event;
|
package mops.gruppen2.domain.event;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
import lombok.experimental.NonFinal;
|
import lombok.experimental.NonFinal;
|
||||||
|
|
||||||
@Value
|
@Value
|
||||||
@NonFinal
|
@NonFinal
|
||||||
|
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
|
||||||
public class Event {
|
public class Event {
|
||||||
long event_id;
|
long event_id;
|
||||||
long group_id;
|
long group_id;
|
||||||
|
|||||||
@ -12,14 +12,12 @@ public class GroupService {
|
|||||||
/**
|
/**
|
||||||
* Konstruiert eine vollständige Gruppe aus Events, welche dieselbe Gruppe betreffen.
|
* Konstruiert eine vollständige Gruppe aus Events, welche dieselbe Gruppe betreffen.
|
||||||
*
|
*
|
||||||
* @param event Initiales CreateGroup-Event
|
* @param eventList Die Events für diese Gruppe
|
||||||
* @param eventList Die restlichen Events für diese Gruppe
|
|
||||||
* @return Gruppe auf aktuellem Stand
|
* @return Gruppe auf aktuellem Stand
|
||||||
*/
|
*/
|
||||||
Group buildGroupFromEvents(List<Event> eventList) {
|
Group buildGroupFromEvents(List<Event> eventList) {
|
||||||
Group newGroup = new Group();
|
Group newGroup = new Group();
|
||||||
newGroup.apply(eventList);
|
newGroup.apply(eventList);
|
||||||
|
|
||||||
return newGroup;
|
return newGroup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,11 @@
|
|||||||
package mops.gruppen2.service;
|
package mops.gruppen2.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import mops.gruppen2.domain.event.Event;
|
||||||
|
import mops.gruppen2.repository.EventRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -7,4 +13,18 @@ import org.springframework.stereotype.Service;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class SerializationService {
|
public class SerializationService {
|
||||||
|
|
||||||
|
private final EventRepository eventStore;
|
||||||
|
private final Logger log = LoggerFactory.getLogger(SerializationService.class);
|
||||||
|
|
||||||
|
public SerializationService(EventRepository eventStore) {
|
||||||
|
this.eventStore = eventStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String serializeEvent(Event event) throws JsonProcessingException {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String json = mapper.writeValueAsString(event);
|
||||||
|
log.info(json);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user