Integrating spring-boot with RESTEasy -
i trying prototype spring boot application. i'm coming guice jax-rs application, prefer standard jax-rs annotations spring mvc. i've gotten jetty , serving:
@configuration @import({resteasybootstrap.class, springbeanprocessorservletaware.class, httpservletdispatcher.class}) public class embeddedjetty { @bean @singleton public embeddedservletcontainerfactory servletcontainer() { jettyembeddedservletcontainerfactory factory = new jettyembeddedservletcontainerfactory(); factory.setport(9000); factory.setsessiontimeout(10, timeunit.minutes); return factory; } }
however, can't figure out how resteasy hooked correctly. above springbeanprocessorservletaware
bails, seemingly servletcontext
not injected through servletcontextaware
before ends being used:
java.lang.nullpointerexception: null @ org.jboss.resteasy.plugins.spring.springbeanprocessorservletaware.getregistry(springbeanprocessorservletaware.java:30) @ org.jboss.resteasy.plugins.spring.springbeanprocessor.postprocessbeanfactory(springbeanprocessor.java:247) @ org.springframework.context.support.postprocessorregistrationdelegate.invokebeanfactorypostprocessors(postprocessorregistrationdelegate.java:284) @ org.springframework.context.support.postprocessorregistrationdelegate.invokebeanfactorypostprocessors(postprocessorregistrationdelegate.java:174) @ org.springframework.context.support.abstractapplicationcontext.invokebeanfactorypostprocessors(abstractapplicationcontext.java:680) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:522) @ org.springframework.boot.context.embedded.embeddedwebapplicationcontext.refresh(embeddedwebapplicationcontext.java:118) @ org.springframework.boot.springapplication.refresh(springapplication.java:766)
i tried using springcontextloaderlistener
, seems conflict spring-boot annotationconfigembeddedwebapplicationcontext
class.
i'm using spring-boot 1.3.3 , spring-framework 4.3.0.rc1
the other answer won't have resources spring beans, autoconfiguration integrate them properly:
the configuration class:
@configuration @conditionalonwebapplication public class resteasyautoconfigurer { private environment environment; @bean(name = "resteasydispatcher") public servletregistrationbean resteasyservletregistration() { servletregistrationbean registrationbean = new servletregistrationbean(new httpservletdispatcher(), getprefix() + "/*"); registrationbean.setinitparameters(immutablemap.of("resteasy.servlet.mapping.prefix", "/rs/")); // set prefix here registrationbean.setloadonstartup(1); return registrationbean; } @bean(destroymethod = "cleanup") public static resteasyspringinitializer resteasyspringinitializer() { return new resteasyspringinitializer(); } @bean // use spring boot configured jackson public customresteasyjackson2provider jackson2provider(objectmapper mapper) { return new customresteasyjackson2provider(mapper); } public static class resteasyspringinitializer implements servletcontextinitializer, applicationcontextaware, beanfactorypostprocessor { private resteasydeployment deployment; private configurableapplicationcontext applicationcontext; private configurablelistablebeanfactory beanfactory; public void cleanup() { deployment.stop(); } @override public void onstartup(servletcontext servletcontext) throws servletexception { listenerbootstrap config = new listenerbootstrap(servletcontext); deployment = config.createdeployment(); deployment.start(); servletcontext.setattribute(resteasyproviderfactory.class.getname(), deployment.getproviderfactory()); servletcontext.setattribute(dispatcher.class.getname(), deployment.getdispatcher()); servletcontext.setattribute(registry.class.getname(), deployment.getregistry()); springbeanprocessor processor = new springbeanprocessor(deployment.getdispatcher(), deployment.getregistry(), deployment.getproviderfactory()); processor.postprocessbeanfactory(beanfactory); applicationcontext.addapplicationlistener(processor); } @override public void postprocessbeanfactory(configurablelistablebeanfactory beanfactory) throws beansexception { this.beanfactory = beanfactory; } @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { this.applicationcontext = (configurableapplicationcontext) applicationcontext; } } }
and jackson provider:
@provider @consumes({"application/*+json", "text/json"}) @produces({"application/*+json", "text/json"}) public class customresteasyjackson2provider extends resteasyjackson2provider { private objectmapper mapper; public customresteasyjackson2provider(objectmapper mapper) { this.mapper = mapper; } @override public objectmapper locatemapper(class<?> type, mediatype mediatype) { return optional.ofnullable(_mapperconfig.getconfiguredmapper()).orelse(mapper); } }
note: working configuration spring boot 1.3.3 / resteasy 3.0.16
Comments
Post a Comment