Embedded JAX-RS CDI (Undertow, RestEasy & Weld)

只愿长相守 提交于 2019-12-13 03:46:22

问题


I've been trying in the last couple of days to setup an embedded Undertow server, with JAX-RS, using RestEasy, and CDI using weld.

No matter what I do, nothing seems to work. I've read through every possible answer, and demo available I could find; Nothing worked!

The JAX-RS resource:

@Path("/")
@Produces(MediaType.TEXT_PLAIN)
@RequestScoped
public class EchoResource {
    @Inject
    @Named("upperCase")
    private UpperCaseTextProcessing upperCaseTextProcessing;

    @Inject
    @Named("lowerCase")
    private LowerCaseTextProcessing lowerCaseTextProcessing;

    @Inject
    BeanManager manager;

    @GET
    public void greet( @Suspended AsyncResponse response, @BeanParam Aggregator queryParams ) {
        final Response.ResponseBuilder responseBuilder = Response.ok();

        if ( queryParams.async ) {
            CompletableFuture.runAsync( () -> {
                try {
                    Thread.sleep( 1500 );
                }
                catch ( InterruptedException e ) {
                    e.printStackTrace();
                }

                responseBuilder.entity( upperCaseTextProcessing.processText( queryParams.message ) );

                response.resume( responseBuilder.build() );
            } );
        }
        else {
            responseBuilder.entity( upperCaseTextProcessing.processText( queryParams.message ) );

            response.resume( responseBuilder.build() );
        }
    }

    public static class Aggregator {
        @QueryParam("async")
        public boolean async;

        @QueryParam("msg")
        public String message;

        @QueryParam("lower")
        @DefaultValue("false")
        public boolean lower;
    }
}

The "UpperCaseTextProcessing" bean:

@ApplicationScoped
@Named( "upperCase" )
public class UpperCaseTextProcessing implements TextProcessing {
    public UpperCaseTextProcessing() {}

    @Override
    public String processText( String text ) {
        return text.toUpperCase();
    }
}

I've used http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_undertow as a reference and the injection there works, but there are two things which make this example partially ok for me

  1. The example there is using a simple HttpServlet, and not a proper JAX-RS servlet.
  2. The "BeanManager" is injected, but trying to inject my own object fails miserably (used with @ApplicationScope)

I've setup a complete repo, with tests to ease to process of helping point me to my error.

https://github.com/eladchen/rest-easy-cdi

For bonus points, I've been wondering if it is possible to work with Weld in both contexts SE and EE, and if so, how?

来源:https://stackoverflow.com/questions/53180370/embedded-jax-rs-cdi-undertow-resteasy-weld

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!