Skip to content

Commit 375f902

Browse files
committed
#1061 - Reenable default codecs for WebFlux configuration.
Spring WebFlux now correctly handles custom codecs. This means we can stop disabling them. This change includes more test cases, verifying Spring MVC configuration for hypermedia as well, verifying both Web MVC and WebFlux are properly and consistently configured. Probably supercedes: #1047
1 parent 28ecf0c commit 375f902

File tree

5 files changed

+493
-16
lines changed

5 files changed

+493
-16
lines changed

src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,14 @@ static class HypermediaWebFluxConfigurer implements WebFluxConfigurer {
124124
@Override
125125
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
126126

127-
CodecConfigurer.CustomCodecs customCodecs = configurer.customCodecs();
128-
129127
this.hypermediaTypes.forEach(hypermedia -> {
130128

131-
MimeType[] mimeTypes = hypermedia.getMediaTypes().toArray(new MimeType[0]);
132-
133129
ObjectMapper objectMapper = hypermedia.configureObjectMapper(this.mapper.copy());
134-
customCodecs.encoder(new Jackson2JsonEncoder(objectMapper, mimeTypes));
135-
customCodecs.decoder(new Jackson2JsonDecoder(objectMapper, mimeTypes));
130+
MimeType[] mimeTypes = hypermedia.getMediaTypes().toArray(new MimeType[0]);
131+
132+
configurer.customCodecs().encoder(new Jackson2JsonEncoder(objectMapper, mimeTypes));
133+
configurer.customCodecs().decoder(new Jackson2JsonDecoder(objectMapper, mimeTypes));
136134
});
137-
138-
customCodecs.encoder(CharSequenceEncoder.allMimeTypes());
139-
customCodecs.decoder(StringDecoder.allMimeTypes());
140-
141-
configurer.registerDefaults(false);
142135
}
143136
}
144137
}

src/main/java/org/springframework/hateoas/mediatype/collectionjson/Jackson2CollectionJsonModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
* @author Greg Turnquist
6666
* @author Oliver Drotbohm
6767
*/
68-
class Jackson2CollectionJsonModule extends SimpleModule {
68+
public class Jackson2CollectionJsonModule extends SimpleModule {
6969

7070
private static final long serialVersionUID = -6540574644565592709L;
7171

src/main/java/org/springframework/hateoas/mediatype/hal/forms/Jackson2HalFormsModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* @author Greg Turnquist
7474
* @author Oliver Gierke
7575
*/
76-
class Jackson2HalFormsModule extends SimpleModule {
76+
public class Jackson2HalFormsModule extends SimpleModule {
7777

7878
private static final long serialVersionUID = -4496351128468451196L;
7979

src/test/java/org/springframework/hateoas/config/HypermediaWebFluxConfigurerTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.web.bind.annotation.RequestBody;
5151
import org.springframework.web.bind.annotation.RestController;
5252
import org.springframework.web.reactive.config.EnableWebFlux;
53+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5354

5455
/**
5556
* @author Greg Turnquist
@@ -258,12 +259,17 @@ void registeringAllHypermediaTypesShouldAllowCreatingThroughAllFormats() {
258259
* @see #728
259260
*/
260261
@Test
261-
void callingForUnregisteredMediaTypeShouldFail() {
262+
void callingForUnregisteredMediaTypeShouldFallBackToJackson() {
262263

263264
setUp(HalWebFluxConfig.class);
264265

265-
this.testClient.get().uri("/").accept(MediaTypes.UBER_JSON).exchange().expectStatus().is4xxClientError()
266-
.returnResult(String.class).getResponseBody().as(StepVerifier::create).verifyComplete();
266+
this.testClient.get().uri("/").accept(MediaTypes.UBER_JSON) //
267+
.exchange() //
268+
.expectStatus().isOk() //
269+
.returnResult(String.class).getResponseBody() //
270+
.as(StepVerifier::create) //
271+
.expectNext("{\"links\":[{\"rel\":\"self\",\"href\":\"/\"},{\"rel\":\"employees\",\"href\":\"/employees\"}]}")
272+
.verifyComplete();
267273
}
268274

269275
/**

0 commit comments

Comments
 (0)