Skip to content

Commit

Permalink
Update Java sample to register Gson mapper as a Bean/Singleton (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephcummings authored Feb 8, 2024
1 parent 53a7617 commit d8d4376
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Quickstart/Java/esdb-sample-springboot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.eventstore:db-client-java:5.2.0'
implementation 'com.eventstore:db-client-java:5.3.0'
implementation 'com.google.code.gson:gson:2.10.1'
}

Expand Down
2 changes: 1 addition & 1 deletion Quickstart/Java/esdb-sample-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>com.eventstore</groupId>
<artifactId>db-client-java</artifactId>
<version>5.2.0</version>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.example.esdbsamplespringboot;

import com.google.gson.Gson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}

@Bean
public Gson GsonMapper() {
return new Gson();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@
import org.springframework.web.bind.annotation.ResponseBody;
import com.eventstore.dbclient.*;
import com.google.gson.Gson;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloWorldController {
private static final String VISITORS_STREAM = "visitors-stream";
private static final Gson gson = new Gson();
private final EventStoreDBClient eventStore;
private final Gson gsonMapper;

@Autowired
public HelloWorldController(EventStoreDBClient eventStore) {
public HelloWorldController(EventStoreDBClient eventStore, Gson gsonMapper) {
this.eventStore = eventStore;
this.gsonMapper = gsonMapper;
}

@ResponseBody
@GetMapping("/hello-world")
public ResponseEntity<?> sayHello(@RequestParam(name="visitor", required=false, defaultValue="Visitor") String visitor) {
@GetMapping("/hello-world")
public ResponseEntity<?> sayHello(@RequestParam(name = "visitor", required = false, defaultValue = "Visitor") String visitor) {
try {
VisitorGreeted visitorGreeted = new VisitorGreeted(visitor);

byte[] vgBytes = gsonMapper.toJson(visitorGreeted).getBytes();
EventData event = EventData
.builderAsJson("VisitorGreeted", visitorGreeted)
.builderAsJson("VisitorGreeted", vgBytes)
.build();

WriteResult writeResult = eventStore
Expand All @@ -45,7 +49,7 @@ public ResponseEntity<?> sayHello(@RequestParam(name="visitor", required=false,

List<String> visitorsGreeted = new ArrayList<>();
for (ResolvedEvent re : eventStream.getEvents()) {
VisitorGreeted vg = gson.fromJson(
VisitorGreeted vg = gsonMapper.fromJson(
new String(re.getOriginalEvent().getEventData()),
VisitorGreeted.class);

Expand All @@ -61,7 +65,7 @@ public ResponseEntity<?> sayHello(@RequestParam(name="visitor", required=false,
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
}

static class VisitorGreeted {
private String visitor;
Expand Down

0 comments on commit d8d4376

Please sign in to comment.