Skip to content

Commit 47bed56

Browse files
author
Dave Syer
authored
Flesh out the docs in the Spring module a bit (#355)
Signed-off-by: Dave Syer <[email protected]>
1 parent baba37c commit 47bed56

File tree

1 file changed

+143
-1
lines changed

1 file changed

+143
-1
lines changed

docs/spring.md

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ For Maven based projects, use the following dependency:
2121
</dependency>
2222
```
2323

24+
plus whatever you need to support your use case (e.g. `spring-boot-starter-webflux` for reactive HTTP).
25+
2426
## Introduction
2527

2628
This module provides classes and interfaces that can be used by
@@ -38,7 +40,147 @@ details).
3840

3941
## Examples
4042

41-
Check out the samples:
43+
### Spring MVC
44+
45+
There is a `CloudEventHttpMessageConverter` that you can register for Spring MVC:
46+
47+
```java
48+
@Configuration
49+
public static class CloudEventHandlerConfiguration implements WebMvcConfigurer {
50+
51+
@Override
52+
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
53+
converters.add(0, new CloudEventHttpMessageConverter());
54+
}
55+
56+
}
57+
```
58+
59+
With this in place you can write a `@RestController` with `CloudEvent` inputs or outputs, and the conversion will be handled by Spring. Example "echo" endpoint:
60+
61+
```java
62+
@PostMapping("/echo")
63+
public CloudEvent ce(@RequestBody CloudEvent event) {
64+
return CloudEventBuilder.from(event)
65+
.withId(UUID.randomUUID().toString())
66+
.withSource(URI.create("https://spring.io/foos"))
67+
.withType("io.spring.event.Foo")
68+
.withData(event.getData().toBytes())
69+
.build();
70+
}
71+
```
72+
73+
Both structured and binary events are supported. So if you know that the `CloudEvent` is in binary mode and the data can be converted to a `Foo`, then you can also use the `CloudEventHttpUtils` to deal with HTTP headers and stick to POJOs in the handler method. Example:
74+
75+
```java
76+
@PostMapping("/echo")
77+
public ResponseEntity<Foo> echo(@RequestBody Foo foo, @RequestHeader HttpHeaders headers) {
78+
CloudEvent attributes = CloudEventHttpUtils.fromHttp(headers)
79+
.withId(UUID.randomUUID().toString())
80+
.withSource(URI.create("https://spring.io/foos"))
81+
.withType("io.spring.event.Foo")
82+
.build();
83+
HttpHeaders outgoing = CloudEventHttpUtils.toHttp(attributes);
84+
return ResponseEntity.ok().headers(outgoing).body(foo);
85+
}
86+
```
87+
88+
### Spring Webflux
89+
90+
If you are using Spring Webflux instead of Spring MVC you can use the same patterns, but the configuration is different. In this case we have a pair of readers and writers that you can register with the `CodecCustomizer`:
91+
92+
```java
93+
@Configuration
94+
public static class CloudEventHandlerConfiguration implements CodecCustomizer {
95+
96+
@Override
97+
public void customize(CodecConfigurer configurer) {
98+
configurer.customCodecs().register(new CloudEventHttpMessageReader());
99+
configurer.customCodecs().register(new CloudEventHttpMessageWriter());
100+
}
101+
102+
}
103+
```
104+
105+
Then you can write similar code to the MVC example above, but with reactive signatures. Example echo endpoint:
106+
107+
```java
108+
@PostMapping("/event")
109+
public Mono<CloudEvent> event(@RequestBody Mono<CloudEvent> body) {
110+
return body.map(event -> CloudEventBuilder.from(event)
111+
.withId(UUID.randomUUID().toString())
112+
.withSource(URI.create("https://spring.io/foos"))
113+
.withType("io.spring.event.Foo")
114+
.withData(event.getData().toBytes()).build());
115+
}
116+
```
117+
118+
### Messaging
119+
120+
Spring Messaging is applicable in a wide range of use cases including WebSockets, JMS, Apache Kafka, RabbitMQ and others. It is also a core part of the Spring Cloud Function and Spring Cloud Stream libraries, so those are natural tools to use to build applications that use Cloud Events. The core abstraction in Spring is the `Message` which carries headers and a payload, just like a `CloudEvent`. Since the mapping is quite direct it makes sense to have a set of converters for Spring applications, so you can consume and produce `CloudEvents`, by treating them as `Messages`. This project provides a converter that you can register in a Spring Messaging application:
121+
122+
```java
123+
@Configuration
124+
public static class CloudEventMessageConverterConfiguration {
125+
@Bean
126+
public CloudEventMessageConverter cloudEventMessageConverter() {
127+
return new CloudEventMessageConverter();
128+
}
129+
}
130+
```
131+
132+
A simple echo with Spring Cloud Function could then be written as:
133+
134+
```java
135+
@Bean
136+
public Function<CloudEvent, CloudEvent> events() {
137+
return event -> CloudEventBuilder.from(event)
138+
.withId(UUID.randomUUID().toString())
139+
.withSource(URI.create("https://spring.io/foos"))
140+
.withType("io.spring.event.Foo")
141+
.withData(event.getData().toBytes())
142+
.build();
143+
}
144+
```
145+
146+
(If the application was a webapp with `spring-cloud-function-web` you would need the HTTP converters or codecs as well, per the example above.)
147+
148+
### Generic Encoder and Decoder
149+
150+
Some applications present Cloud Events as binary data, but do not have "headers" like in HTTP or messages. For those use cases there is a lower level construct in Spring, and this project provides implementations in the form of `CloudEventEncoder` and `CloudEventDecoder`. Since the headers are not available in the surrounding abstraction, these only support _structured_ Cloud Events, where the attributes and data are packed together in the same byte buffer. As an example in an RSockets application you can register them like this:
151+
152+
```java
153+
@Bean
154+
@Order(-1)
155+
public RSocketStrategiesCustomizer cloudEventsCustomizer() {
156+
return new RSocketStrategiesCustomizer() {
157+
@Override
158+
public void customize(Builder strategies) {
159+
strategies.encoder(new CloudEventEncoder());
160+
strategies.decoder(new CloudEventDecoder());
161+
}
162+
};
163+
164+
}
165+
```
166+
167+
and then a simple echo endpoint could be written like this:
168+
169+
```java
170+
@MessageMapping("event")
171+
public Mono<CloudEvent> event(@RequestBody Mono<CloudEvent> body) {
172+
return body.map(event -> CloudEventBuilder.from(event)
173+
.withId(UUID.randomUUID().toString())
174+
.withSource(URI.create("https://spring.io/foos"))
175+
.withType("io.spring.event.Foo")
176+
.withData(event.getData().toBytes())
177+
.build());
178+
}
179+
```
180+
181+
### More
182+
183+
Check out the integration tests and samples:
42184

43185
- [spring-reactive](https://github.com/cloudevents/sdk-java/tree/master/examples/spring-reactive)
44186
shows how to receive and send CloudEvents through HTTP using Spring Boot and

0 commit comments

Comments
 (0)