Skip to content

Commit cb1fd47

Browse files
Jackson3
Signed-off-by: David Strömner <david.stromner@stralfors.se>
1 parent 346dfc6 commit cb1fd47

24 files changed

Lines changed: 326 additions & 306 deletions

File tree

examples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020

2121
<modules>
2222
<module>kafka</module>
23-
<module>restful-ws-quarkus</module>
23+
<!--<module>restful-ws-quarkus</module> TODO!-->
2424
<module>restful-ws-microprofile-liberty</module>
2525
<module>vertx</module>
2626
<module>basic-http</module>
2727
<module>restful-ws-spring-boot</module>
2828
<module>amqp-proton</module>
2929
<module>spring-reactive</module>
3030
<module>spring-rsocket</module>
31-
<module>spring-function</module>
31+
<!--<module>spring-function</module> TODO!-->
3232
<module>rocketmq</module>
3333
</modules>
3434

examples/restful-ws-quarkus/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<artifactId>cloudevents-restful-ws-quarkus-example</artifactId>
1212
<properties>
1313
<module-name>cloudevents.example.restful.ws.quarkus</module-name>
14-
<quarkus-plugin.version>1.10.3.Final</quarkus-plugin.version>
14+
<quarkus-plugin.version>3.30.4</quarkus-plugin.version>
1515
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
1616
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
17-
<quarkus.platform.version>1.10.3.Final</quarkus.platform.version>
17+
<quarkus.platform.version>3.30.4</quarkus.platform.version>
1818
</properties>
1919
<dependencyManagement>
2020
<dependencies>

examples/restful-ws-spring-boot/src/main/java/io/cloudevents/examples/springboot/MainResource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
import org.springframework.web.bind.annotation.PostMapping;
2828
import org.springframework.web.bind.annotation.RequestBody;
2929
import org.springframework.web.bind.annotation.RestController;
30-
import tools.jackson.databind.ObjectMapper;
30+
import tools.jackson.databind.json.JsonMapper;
3131

3232
import static io.cloudevents.core.CloudEventUtils.mapData;
3333

3434
@RestController
3535
public class MainResource {
3636
public static final String HAPPY_BIRTHDAY_EVENT_TYPE = "happybirthday.myapplication";
3737
@Autowired
38-
private ObjectMapper objectMapper;
38+
private JsonMapper jsonMapper;
3939

4040
@PostMapping("/happy_birthday")
4141
public ResponseEntity handleHappyBirthdayEvent(@RequestBody CloudEvent inputEvent) {
@@ -45,7 +45,7 @@ public ResponseEntity handleHappyBirthdayEvent(@RequestBody CloudEvent inputEven
4545
.body("Event type should be \"" + HAPPY_BIRTHDAY_EVENT_TYPE + "\" but is \"" + inputEvent.getType() + "\"");
4646
}
4747

48-
PojoCloudEventData<User> cloudEventData = mapData(inputEvent, PojoCloudEventDataMapper.from(objectMapper, User.class));
48+
PojoCloudEventData<User> cloudEventData = mapData(inputEvent, PojoCloudEventDataMapper.from(jsonMapper, User.class));
4949

5050
if (cloudEventData == null) {
5151
return ResponseEntity.badRequest().contentType(MediaType.TEXT_PLAIN).body("Event should contain the user");
@@ -54,7 +54,7 @@ public ResponseEntity handleHappyBirthdayEvent(@RequestBody CloudEvent inputEven
5454
User user = cloudEventData.getValue();
5555
user.setAge(user.getAge() + 1);
5656

57-
CloudEvent outputEvent = CloudEventBuilder.from(inputEvent).withData(PojoCloudEventData.wrap(user, objectMapper::writeValueAsBytes)).build();
57+
CloudEvent outputEvent = CloudEventBuilder.from(inputEvent).withData(PojoCloudEventData.wrap(user, jsonMapper::writeValueAsBytes)).build();
5858

5959
return ResponseEntity.ok(outputEvent);
6060
}

examples/spring-function/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<properties>
1515
<module-name>cloudevents.example.spring.function</module-name>
1616
<spring-boot.version>4.0.0</spring-boot.version>
17+
<spring-cloud.version>2025.1.0</spring-cloud.version>
1718
</properties>
1819

1920
<dependencyManagement>
@@ -25,19 +26,29 @@
2526
<type>pom</type>
2627
<scope>import</scope>
2728
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-dependencies</artifactId>
32+
<version>${spring-cloud.version}</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
2836
</dependencies>
2937
</dependencyManagement>
3038

3139
<dependencies>
3240
<dependency>
3341
<groupId>org.springframework.cloud</groupId>
3442
<artifactId>spring-cloud-function-web</artifactId>
35-
<version>3.1.1</version>
3643
</dependency>
3744
<dependency>
3845
<groupId>org.springframework.boot</groupId>
3946
<artifactId>spring-boot-starter-webflux</artifactId>
4047
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-gson</artifactId>
51+
</dependency>
4152
<dependency>
4253
<groupId>io.cloudevents</groupId>
4354
<artifactId>cloudevents-spring</artifactId>

examples/spring-function/src/main/java/io/cloudevents/examples/spring/DemoApplication.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import org.springframework.boot.SpringApplication;
88
import org.springframework.boot.autoconfigure.SpringBootApplication;
9-
import org.springframework.boot.web.codec.CodecCustomizer;
9+
import org.springframework.boot.http.codec.CodecCustomizer;
1010
import org.springframework.context.annotation.Bean;
1111
import org.springframework.context.annotation.Configuration;
1212
import org.springframework.http.codec.CodecConfigurer;
@@ -19,8 +19,7 @@
1919

2020
@SpringBootApplication
2121
public class DemoApplication {
22-
23-
public static void main(String[] args) throws Exception {
22+
static void main(String[] args) {
2423
SpringApplication.run(DemoApplication.class, args);
2524
}
2625

@@ -52,13 +51,10 @@ public CloudEventMessageConverter cloudEventMessageConverter() {
5251
*/
5352
@Configuration
5453
public static class CloudEventHandlerConfiguration implements CodecCustomizer {
55-
5654
@Override
5755
public void customize(CodecConfigurer configurer) {
5856
configurer.customCodecs().register(new CloudEventHttpMessageReader());
5957
configurer.customCodecs().register(new CloudEventHttpMessageWriter());
6058
}
61-
6259
}
63-
6460
}
Lines changed: 93 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,121 @@
11
package io.cloudevents.examples.spring;
22

3-
import java.net.URI;
4-
3+
import org.junit.jupiter.api.BeforeEach;
54
import org.junit.jupiter.api.Test;
6-
7-
import org.springframework.beans.factory.annotation.Autowired;
85
import org.springframework.boot.test.context.SpringBootTest;
96
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
10-
import org.springframework.boot.test.web.client.TestRestTemplate;
11-
import org.springframework.boot.web.server.LocalServerPort;
12-
import org.springframework.http.HttpHeaders;
7+
import org.springframework.boot.test.web.server.LocalServerPort;
138
import org.springframework.http.HttpStatus;
149
import org.springframework.http.MediaType;
15-
import org.springframework.http.RequestEntity;
16-
import org.springframework.http.ResponseEntity;
10+
import org.springframework.test.web.servlet.client.ExchangeResult;
11+
import org.springframework.test.web.servlet.client.RestTestClient;
12+
13+
import java.util.Map;
1714

1815
import static org.assertj.core.api.Assertions.assertThat;
1916

2017
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
21-
public class DemoApplicationTests {
22-
23-
@Autowired
24-
private TestRestTemplate rest;
18+
class DemoApplicationTests {
19+
String NEW_BODY = "{\"data\": {\"value\": \"Dave\" }}";
20+
private static final String BODY = "{\"value\":\"Dave\"}";
21+
private RestTestClient rest;
2522

2623
@LocalServerPort
2724
private int port;
2825

26+
@BeforeEach
27+
void setUp() {
28+
rest = RestTestClient
29+
.bindToServer()
30+
.baseUrl(String.format("http://localhost:%d", port))
31+
.build();
32+
}
33+
2934
@Test
3035
void echoWithCorrectHeaders() {
31-
32-
ResponseEntity<String> response = rest
33-
.exchange(RequestEntity.post(URI.create("http://localhost:" + port + "/foos")) //
34-
.header("ce-id", "12345") //
35-
.header("ce-specversion", "1.0") //
36-
.header("ce-type", "io.spring.event") //
37-
.header("ce-source", "https://spring.io/events") //
38-
.contentType(MediaType.APPLICATION_JSON) //
39-
.body("{\"value\":\"Dave\"}"), String.class);
40-
41-
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
42-
assertThat(response.getBody()).isEqualTo("{\"value\":\"Dave\"}");
43-
44-
HttpHeaders headers = response.getHeaders();
45-
46-
assertThat(headers).containsKey("ce-id");
47-
assertThat(headers).containsKey("ce-source");
48-
assertThat(headers).containsKey("ce-type");
49-
50-
assertThat(headers.getFirst("ce-id")).isNotEqualTo("12345");
51-
assertThat(headers.getFirst("ce-type")).isEqualTo("io.spring.event.Foo");
52-
assertThat(headers.getFirst("ce-source")).isEqualTo("https://spring.io/foos");
53-
36+
ExchangeResult response = rest.post()
37+
.uri("/foos")
38+
.header("ce-id", "12345")
39+
.header("ce-specversion", "1.0")
40+
.header("ce-type", "io.spring.event")
41+
.header("ce-source", "https://spring.io/events")
42+
.contentType(MediaType.APPLICATION_JSON)
43+
.body(NEW_BODY)
44+
.exchange()
45+
.returnResult(String.class);
46+
47+
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
48+
assertThat(response.getResponseBodyContent()).isEqualTo(BODY.getBytes());
49+
50+
Map<String, String> headers = response.getResponseHeaders().toSingleValueMap();
51+
52+
assertThat(headers)
53+
.containsKey("ce-id")
54+
.containsKey("ce-source")
55+
.containsKey("ce-type")
56+
.doesNotContainEntry("ce-id", "12345")
57+
.containsEntry("ce-type", "io.spring.event.Foo")
58+
.containsEntry("ce-source", "https://spring.io/foos");
5459
}
5560

5661
@Test
5762
void structuredRequestResponseEvents() {
58-
59-
ResponseEntity<String> response = rest
60-
.exchange(RequestEntity.post(URI.create("http://localhost:" + port + "/event")) //
61-
.contentType(new MediaType("application", "cloudevents+json")) //
62-
.body("{" //
63-
+ "\"id\":\"12345\"," //
64-
+ "\"specversion\":\"1.0\"," //
65-
+ "\"type\":\"io.spring.event\"," //
66-
+ "\"source\":\"https://spring.io/events\"," //
67-
+ "\"data\":{\"value\":\"Dave\"}}"),
68-
String.class);
69-
70-
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
71-
assertThat(response.getBody()).isEqualTo("{\"value\":\"Dave\"}");
72-
73-
HttpHeaders headers = response.getHeaders();
74-
75-
assertThat(headers).containsKey("ce-id");
76-
assertThat(headers).containsKey("ce-source");
77-
assertThat(headers).containsKey("ce-type");
78-
79-
assertThat(headers.getFirst("ce-id")).isNotEqualTo("12345");
80-
assertThat(headers.getFirst("ce-type")).isEqualTo("io.spring.event.Foo");
81-
assertThat(headers.getFirst("ce-source")).isEqualTo("https://spring.io/foos");
82-
63+
ExchangeResult response = rest.post()
64+
.uri("/event")
65+
.contentType(new MediaType("application", "cloudevents+json"))
66+
.body(String.format(
67+
"""
68+
{
69+
"id": "12345",
70+
"specversion": "1.0",
71+
"type": "io.spring.event",
72+
"source": "https://spring.io/events",
73+
"data": %s
74+
}
75+
""", BODY
76+
))
77+
.exchange()
78+
.returnResult(String.class);
79+
80+
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
81+
assertThat(response.getResponseBodyContent()).isEqualTo(BODY.getBytes());
82+
83+
Map<String, String> headers = response.getResponseHeaders().toSingleValueMap();
84+
85+
assertThat(headers)
86+
.containsKey("ce-id")
87+
.containsKey("ce-source")
88+
.containsKey("ce-type")
89+
.doesNotContainEntry("ce-id", "12345")
90+
.containsEntry("ce-type", "io.spring.event.Foo")
91+
.containsEntry("ce-source", "https://spring.io/foos");
8392
}
8493

8594
@Test
8695
void requestResponseEvents() {
87-
88-
ResponseEntity<String> response = rest
89-
.exchange(RequestEntity.post(URI.create("http://localhost:" + port + "/event")) //
90-
.header("ce-id", "12345") //
91-
.header("ce-specversion", "1.0") //
92-
.header("ce-type", "io.spring.event") //
93-
.header("ce-source", "https://spring.io/events") //
94-
.contentType(MediaType.APPLICATION_JSON) //
95-
.body("{\"value\":\"Dave\"}"), String.class);
96-
97-
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
98-
assertThat(response.getBody()).isEqualTo("{\"value\":\"Dave\"}");
99-
100-
HttpHeaders headers = response.getHeaders();
101-
102-
assertThat(headers).containsKey("ce-id");
103-
assertThat(headers).containsKey("ce-source");
104-
assertThat(headers).containsKey("ce-type");
105-
106-
assertThat(headers.getFirst("ce-id")).isNotEqualTo("12345");
107-
assertThat(headers.getFirst("ce-type")).isEqualTo("io.spring.event.Foo");
108-
assertThat(headers.getFirst("ce-source")).isEqualTo("https://spring.io/foos");
109-
96+
ExchangeResult response = rest.post()
97+
.uri("/event")
98+
.header("ce-id", "12345")
99+
.header("ce-specversion", "1.0")
100+
.header("ce-type", "io.spring.event")
101+
.header("ce-source", "https://spring.io/events")
102+
.contentType(MediaType.APPLICATION_JSON)
103+
.body(BODY)
104+
.exchange()
105+
.returnResult(String.class);
106+
107+
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
108+
assertThat(response.getResponseBodyContent()).isEqualTo(BODY.getBytes());
109+
110+
Map<String, String> headers = response.getResponseHeaders().toSingleValueMap();
111+
112+
assertThat(headers)
113+
.containsKey("ce-id")
114+
.containsKey("ce-source")
115+
.containsKey("ce-type")
116+
.doesNotContainEntry("ce-id", "12345")
117+
.containsEntry("ce-type", "io.spring.event.Foo")
118+
.containsEntry("ce-source", "https://spring.io/foos");
110119
}
111120

112121
}

examples/spring-reactive/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
<groupId>org.springframework.boot</groupId>
3434
<artifactId>spring-boot-starter-webflux</artifactId>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-webtestclient</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-webclient</artifactId>
43+
</dependency>
3644
<dependency>
3745
<groupId>io.cloudevents</groupId>
3846
<artifactId>cloudevents-spring</artifactId>

examples/spring-reactive/src/main/java/io/cloudevents/examples/spring/DemoApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import io.cloudevents.spring.http.CloudEventHttpUtils;
99
import io.cloudevents.spring.webflux.CloudEventHttpMessageReader;
1010
import io.cloudevents.spring.webflux.CloudEventHttpMessageWriter;
11+
import org.springframework.boot.http.codec.CodecCustomizer;
1112
import reactor.core.publisher.Mono;
1213

1314
import org.springframework.boot.SpringApplication;
1415
import org.springframework.boot.autoconfigure.SpringBootApplication;
15-
import org.springframework.boot.web.codec.CodecCustomizer;
1616
import org.springframework.context.annotation.Configuration;
1717
import org.springframework.http.HttpHeaders;
1818
import org.springframework.http.ResponseEntity;
@@ -64,4 +64,4 @@ public void customize(CodecConfigurer configurer) {
6464

6565
}
6666

67-
}
67+
}

examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/DemoApplicationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
10+
import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient;
1011
import org.springframework.http.MediaType;
1112
import org.springframework.test.web.reactive.server.WebTestClient;
1213

1314
import io.cloudevents.CloudEvent;
1415
import io.cloudevents.core.builder.CloudEventBuilder;
1516
import static org.assertj.core.api.Assertions.assertThat;
1617

18+
@AutoConfigureWebTestClient
1719
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
18-
public class DemoApplicationTests {
19-
20+
class DemoApplicationTests {
2021
@Autowired
2122
private WebTestClient rest;
2223

0 commit comments

Comments
 (0)