forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rest Client Reactive: Provide custom ObjectMapper in request scope
With these changes, we can now register custom object mappers when creating a client programmatically: ``` clientAllowsUnknown = RestClientBuilder.newBuilder() .baseUrl(new URL(wireMockServer.baseUrl())) .register(ClientObjectMapperUnknown.class) .build(MyClient.class); ``` where ClientObjectMapperUnknown is: ``` public static class ClientObjectMapperUnknown implements ContextResolver<ObjectMapper> { @OverRide public ObjectMapper getContext(Class<?> type) { return new ObjectMapper() .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); } } ``` I implemented this feature by injecting the rest client context via the interface ClientRestHandler. Then, the rest client context has registered all the context resolvers, so the jackson message reader/writer can get the custom object mappers. Fix quarkusio#26152 Relates quarkusio#26988
- Loading branch information
1 parent
a3c2e03
commit 9b9673b
Showing
16 changed files
with
327 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...rkus/rest/client/reactive/jackson/runtime/serialisers/ClientJacksonMessageBodyReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.rest.client.reactive.jackson.runtime.serialisers; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Function; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext; | ||
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler; | ||
import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
|
||
public class ClientJacksonMessageBodyReader extends JacksonBasicMessageBodyReader implements ClientRestHandler { | ||
|
||
private final ConcurrentMap<ObjectMapper, ObjectReader> contextResolverMap = new ConcurrentHashMap<>(); | ||
private RestClientRequestContext context; | ||
|
||
@Inject | ||
public ClientJacksonMessageBodyReader(ObjectMapper mapper) { | ||
super(mapper); | ||
} | ||
|
||
@Override | ||
public void handle(RestClientRequestContext requestContext) { | ||
this.context = requestContext; | ||
} | ||
|
||
@Override | ||
protected ObjectReader getEffectiveReader() { | ||
if (context == null) { | ||
// no context injected when reader is not running within a rest client context | ||
return super.getEffectiveReader(); | ||
} | ||
|
||
ObjectMapper objectMapper = context.getConfiguration().getFromContext(ObjectMapper.class); | ||
if (objectMapper == null) { | ||
return super.getEffectiveReader(); | ||
} | ||
|
||
return contextResolverMap.computeIfAbsent(objectMapper, new Function<>() { | ||
@Override | ||
public ObjectReader apply(ObjectMapper objectMapper) { | ||
return objectMapper.reader(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.