Spring 4 REST application using Java configuration (no xml) IllegalArgumentException -
this question has answer here:
this driving me crazy. i've done bit of spring coding first time i'm trying configuration in java. i've searched on stackoverflow , other places, , while others have seen problem, none of solutions have worked me.
here source files:
initializer:
public class enhancedcandidateinfoinitializer extends abstractannotationconfigdispatcherservletinitializer { private static final logger logger = loggerfactory.getlogger(enhancedcandidateinfoinitializer.class); @override protected class<?>[] getrootconfigclasses() { logger.info("@@@@@ getrootconfigclasses called - returning null @@@@@"); return null; } @override protected class<?>[] getservletconfigclasses() { logger.info("@@@@@ getservletconfigclasses called returning enhancedcandidateinfowebconfiguration.class @@@@@"); return new class[] { enhancedcandidateinfowebconfiguration.class }; } @override protected string[] getservletmappings() { logger.info("@@@@@ getservletmappings called @@@@@"); return new string[] { "/" }; } }
webmvcconfiguration:
@configuration @enablewebmvc @componentscan(basepackages = {"com.mojorank.restapi"}) public class enhancedcandidateinfowebconfiguration extends webmvcconfigureradapter { private static final logger logger = loggerfactory.getlogger(enhancedcandidateinfowebconfiguration.class); @override public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { logger.info("#### configuration handler called ####"); configurer.enable(); } }
controller:
@restcontroller public class enhancecandidateinfocontroller { @requestmapping("/") public string welcome() {//welcome page, non-rest return "welcome resttemplate example."; } @requestmapping("/hello/{player}") public message message(@pathvariable string player) {//rest endpoint. message msg = new message(player, "hello " + player); return msg; } }
when build , deploy application tomcat, following exception stack trace:
caused by: java.lang.illegalargumentexception: failed register servlet name 'dispatcher'.check if there servlet registered under same name. @ org.springframework.util.assert.notnull(assert.java:115) @ org.springframework.web.servlet.support.abstractdispatcherservletinitializer.registerdispatcherservlet(abstractdispatcherservletinitializer.java:98) @ org.springframework.web.servlet.support.abstractdispatcherservletinitializer.onstartup(abstractdispatcherservletinitializer.java:71) @ org.springframework.web.springservletcontainerinitializer.onstartup(springservletcontainerinitializer.java:169) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5274) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150)
as said, i've searched through stackoverflow , other places , found others had same problem, when tried implement proposed fixes, problem remained. in advance help.
change webmvcconfiguration
method this:
public class enhancedcandidateinfowebconfiguration extends webmvcconfigureradapter { @override public void configureviewresolvers(viewresolverregistry registry) { internalresourceviewresolver ivr=new internalresourceviewresolver(); ivr.setprefix("/web-inf/jsp/"); ivr.setsuffix(".jsp"); ivr.setexposecontextbeansasattributes(true); registry.viewresolver(ivr); } }
Comments
Post a Comment