Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonb still not injectable #5697

Open
hantsy opened this issue Jul 13, 2024 · 6 comments
Open

Jsonb still not injectable #5697

hantsy opened this issue Jul 13, 2024 · 6 comments

Comments

@hantsy
Copy link

hantsy commented Jul 13, 2024

The example project: https://github.com/hantsy/jakartaee11-sandbox/tree/master/rest

  • Java: 21
  • Glassfish v8.0.0-M6 built-in Jersey

I have created a @Provider described like this:

@Provider
public class JsonbContextResolver implements ContextResolver<Jsonb> {
    @Override
    public Jsonb getContext(Class<?> type) {
        JsonbConfig config = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES)
                .withFormatting(true)
                .withNullValues(false);
        return JsonbBuilder.newBuilder().withConfig(config).build();
    }
}

As discussed in here, jakartaee/rest#742, a Jsonb instance should be available in the context.

@Inject Jsonb jsonb;

I tried to inject Jsonb, failed.

@jansupol
Copy link
Contributor

This cannot work as you show. Once the ContextResolver<Jsonb> is at hand, the Jsonb context is obtained based on the supplied (entity) type.

The Spec mandates the following works in every implementation:

@Context Providers providers;
ContextResolver<Jsonb> contextResolver = providers.getContextResolver(Jsonb.class, MediaType.APPLICATION_JSON_TYPE);
Jsonb jsonb = contextResolver.getContext(type);

See Section 10 for mandatory injectable types.

@jansupol
Copy link
Contributor

More specifically, the spec requested the JSON-B Message Body provider to automatically utilize the user-provided ContextResolver<Jsonb> by the JSON-B implementation used by that provider. Using this unified way to obtain the ContextResolver<Jsonb>. The customer needs just to register the ContextResolver without the need of taking care of the propagation of the Jsonb to the JSON-B implementation.

@hantsy
Copy link
Author

hantsy commented Jul 23, 2024

The customer needs just to register the ContextResolver without the need of taking care of the propagation of the Jsonb to the JSON-B implementation.

Here I want to use jsonb bean to do some manual serialization work.

@hantsy
Copy link
Author

hantsy commented Jul 23, 2024

When REST embraces CDI, I hope there is a united way to register beans via ContextResolver and CDI context.

  1. As I expected, when registering via Context resolver, provides beans in CDI context, or
  2. Using CDI @Produces to create the ConextResolver<Jsonb>, make jsonb available in Rest context and CDI context.

BTW, more easily for developers, as some frameworks provide, I would like to register a bean to customize JsonbConfig, then the REST CDI integration module will register ContextResolver<Jsonb> and produce Jsonb.

@jansupol
Copy link
Contributor

I think the plan was to remove ContextResolver from Jakarta REST with CDI. But I am not sure what the replacement was. That is yet to be discussed.

@jansupol
Copy link
Contributor

Maybe not, it is still there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants