I am currently trying to develop a solution within Quarkus that can serialize JSON responses dynamically based on the properties (i.e. query params, headers, etc.) of the associated request. Here's an example of what I am trying to achieve.
- The user sends a
GET
request containing a query paramlang
which specifies the language.
@GET()
@Produces(MediaType.APPLICATION_JSON)
public Content getContent(
@QueryParam("lang")
@DefaultValue("en")
Language language
) {
// Fetch and return content from service
}
- The
Content
class contains fields of typeTranslatableString
, which store keys used for lookups.
public @Value class TranslatableString {
String key;
}
- The associated
JsonSerializer
should now take this custom type and use its key to provide the translation for the requested language.
@ApplicationScoped
public class Serializer extends JsonSerializer<TranslatableString> {
@Inject
TranslationStore translations;
@Override
public void serialize(TranslatableString value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(
translations.get(value.getKey(), /** ??-Language-?? **/ )
);
}
}
Is there some (easy) way to access the user-provided query parameters inside the Jackson JsonSerializer
?
EDIT
This is how I register my custom serializer.
@Singleton
public class ObjectMapperConfig implements ObjectMapperCustomizer {
@Inject
Serializer serializer;
@Override
public void customize(ObjectMapper objectMapper) {
objectMapper.registerModule(
new SimpleModule()
.addSerializer(
TranslatableString.class,
serializer
)
);
}
}