问题
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
- The example there is using a simple HttpServlet, and not a proper JAX-RS servlet.
- 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