Use Controlleradvice precautions, ambiguous @ExceptionHandler method mapped for [class Org.springframework.web.bind.MethodArgumentNotValidException]

Source: Internet
Author: User

Objective

ControllerAdviceVery good, can be the system inside the exception of unified processing. It's easy to use. For example, http://www.cnblogs.com/woshimrf/p/spring-web-400.html

Spring provides a base class ResponseEntityExceptionHandler that can be used to handle this. However, when using the note, do not repeat the definition of exception capture, for the base has been defined, as long as the overwrite is good, do not repeat the declaration of abnormal interception.

Following from StackOverflow

Problem
@ControllerAdvice Public classRestresponseentityexceptionhandlerextendsResponseentityexceptionhandler {PrivateLogger log = Loggerfactory.GetLogger(Restresponseentityexceptionhandler.class);@Autowired    PrivateApplicationContext ApplicationContext;@ExceptionHandler({constraintviolationexception.class})@ResponseStatus(Httpstatus.bad_request)@ResponseBody     PublicErrorWrapperhandleconstraintviolationexception(Constraintviolationexception e) {String FieldName = e.Getconstraintname(); String message =Getresourcemessage(FieldName +". Alreadyexists","Already Exists");return New ErrorWrapper(FieldName +". Error", message); }@ExceptionHandler({methodargumentnotvalidexception.class})@ResponseStatus(Httpstatus.bad_request)@ResponseBody     PublicErrorWrapperhandlemethodargumentnotvalidexception(Methodargumentnotvalidexception e) {return New ErrorWrapper(". Error","Test"); }PrivateStringGetresourcemessage(string key, String defaultmessage) {String message = ApplicationContext.GetMessage(Key,NULL, Locale.Getdefault());if(StringUtils.Isnotempty(message)) {returnMessage }returnDefaultmessage; }}

You will get the following exception:

Org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Org.springframework.web.servlet.mvc.method.annotation.exceptionhandlerexceptionresolver#0 ': Invocation of Init Method failed; Nested exception is java.lang.IllegalStateException:Ambiguous @ExceptionHandler method mapped for [class Org.springframework.web.bind.MethodArgumentNotValidException]: {public com.ca.bean.ErrorWrapper Com.ca.exceptionHandler.RestResponseEntityExceptionHandler.handleMethodArgumentNotValidException ( org.springframework.web.bind.MethodArgumentNotValidException), public final Org.springframework.http.ResponseEntity Org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException ( Java.lang.exception,org.springframework.web.context.request.webrequest)} at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean ( abstractautowirecapablebeanfactory.java:1578) ~[spring-beans-4.3.0.release.jar:4.3.0.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( abstractautowirecapablebeanfactory.java:545) ~[spring-beans-4.3.0.release.jar:4.3.0.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:482) ~[spring-beans-4.3.0.release.jar:4.3.0.release] at Org.springframework.beans.factory.support.abstractbeanfactory$1.getobject (abstractbeanfactory.java:306) ~[ Spring-beans-4.3.0.release.jar:4.3.0.release] At Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.0.release.jar:4.3.0.release] at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:302) ~[ Spring-beans-4.3.0.release.jar:4.3.0.release] At Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:197) ~[ Spring-beans-4.3.0.release.jar: 4.3.0.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons (defaultlistablebeanfactory.java:775) ~[spring-beans-4.3.0.release.jar:4.3.0.release] at Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( abstractapplicationcontext.java:861) ~[spring-context-4.3.0.release.jar:4.3.0.release] at Org.springframework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:541) ~[ Spring-context-4.3.0.release.jar:4.3.0.release] At Org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (contextloader.java:444) ~[spring-web-4.3.0.release.jar:4.3.0.release] At Org.springframework.web.context.ContextLoader.initWebApplicationContext (contextloader.java:326) ~[ Spring-web-4.3.0.release.jar:4.3.0.release] At Org.springframework.web.context.ContextLoaderListener.contextInitialized (contextloaderlistener.java:107) [ Spring-web-4.3.0.release. Jar:4.3.0.release] at Org.apache.catalina.core.StandardContext.listenerStart (standardcontext.java:5099) [ CATALINA.JAR:7.0.70] at org.apache.catalina.core.StandardContext.startInternal (standardcontext.java:5615) [ CATALINA.JAR:7.0.70] at Org.apache.catalina.util.LifecycleBase.start (lifecyclebase.java:147) [catalina.jar:7.0.70 ] at Org.apache.catalina.core.containerbase$startchild.call (containerbase.java:1571) [catalina.jar:7.0.70] at ORG.A Pache.catalina.core.containerbase$startchild.call (containerbase.java:1561) [catalina.jar:7.0.70] at Java.util.concurrent.FutureTask.run (futuretask.java:266) [na:1.8.0_92] at Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1142) [na:1.8.0_92] at Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:617) [na:1.8.0_92] at Java.lang.Thread.run (thread.java:745) [na:1.8.0_92]caused by:java.lang.IllegalStateException:Ambiguous @ Exceptionhandler method mapped for [Class Org.springframewOrk.web.bind.MethodArgumentNotValidException]: {public com.ca.bean.ErrorWrapper Com.ca.exceptionHandler.RestResponseEntityExceptionHandler.handleMethodArgumentNotValidException ( org.springframework.web.bind.MethodArgumentNotValidException), public final Org.springframework.http.ResponseEntity Org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException ( Java.lang.exception,org.springframework.web.context.request.webrequest)} at Org.springframework.web.method.annotation.ExceptionHandlerMethodResolver.addExceptionMapping ( exceptionhandlermethodresolver.java:109) ~[spring-web-4.3.0.release.jar:4.3.0.release] at Org.springframework.web.method.annotation.exceptionhandlermethodresolver.<init> ( exceptionhandlermethodresolver.java:76) ~[spring-web-4.3.0.release.jar:4.3.0.release] at Org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.initExceptionHandlerAdviceCache (exceptionhandlerexceptionresolver.java:265) ~[spring-webmvc-4.3.0.release.jar:4.3.0.release] At Org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.afterPropertiesSet ( exceptionhandlerexceptionresolver.java:241) ~[spring-webmvc-4.3.0.release.jar:4.3.0.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods ( abstractautowirecapablebeanfactory.java:1637) ~[spring-beans-4.3.0.release.jar:4.3.0.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean ( abstractautowirecapablebeanfactory.java:1574) ~[spring-beans-4.3.0.release.jar:4.3.0.release] ... Common frames omitted

The exception stack indicates that the exception interception is defined repeatedly. So what if you ResponseEntityExceptionHandler want to modify something after you inherit it?

Answer

Just rewrite the parent class method. Do not declare an interception exception on the overridden method.

For an ResponseEntityExceptionHandler already defined exception, there is no need to redefine it, as long as you override the method.

Use Controlleradvice precautions, ambiguous @ExceptionHandler method mapped for [class Org.springframework.web.bind.MethodArgumentNotValidException]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.