Springmvc Exception Handling

Source: Internet
Author: User

Springmvc the exception that is handled by the Handlerexceptionresolver handler, including handler mappings, data binding, and exceptions that occur when the method executes, The implementation classes for the Handlerexceptionresolver provided by SPRINGMVC are as follows:

Dispatcheservlet the handlerexceptionresolver that are installed by default:

Not using <mvc:annotation-driven/> configuration:

Use of <mvc:annotation-driven/> configuration:

Exceptionhandlerexceptionresolver

It mainly deals with the method defined by @exceptionhandler annotations in handler.

@ExceptionHandler annotation-defined method precedence issues: For example, NullPointerException is occurring, but declared exceptions are runtimeexception and exception, The method that defines the @exceptionhandler annotation that is the least inherited depth is found based on the most recent inheritance of the exception, and the method that marks the runtimeexception.

Package Com.seven.exceptionhandler;import Org.springframework.web.bind.annotation.controlleradvice;import Org.springframework.web.bind.annotation.exceptionhandler;import org.springframework.web.servlet.modelandview;/* * * Created by Hu on 2016/4/4. */@ControllerAdvicepublic class Springmvctestexceptionhandler {    @ExceptionHandler {arithmeticexception.class} Public    Modelandview handlearithmeticexception (Exception e) {        System.out.println ("----> Unexpected:" +e);        Go to the view with the logical name error        Modelandview modelandview=new modelandview ("error");        Modelandview.addobject ("Exception", e);        return modelandview;}    }

Responsestatusexceptionresolver

Find the @responsestatus annotation in the parent class of the exception and exception, and then use the annotation's properties for processing.

Package Com.seven.exceptionhandler;import Org.springframework.http.httpstatus;import org.springframework.web.bind.annotation.responsestatus;/** * Created by Hu on 2016/4/4. */@ResponseStatus (value = Httpstatus.forbidden,reason = "username and password do not match!) ") public class Usernamenotmatchpasswordexception extends runtimeexception{    * * When the program occurs usernamenotmatchpasswordexception this exception,    * Because this exception is triggered with @responsestatus annotations, So it will be parsed by Responsestatusexceptionresolver,    * Then the state of the response is forbidden, the code is 401, no permissions. The reason is "user name and password do not match!" "    * Return to Client * * * *    }

Defaulthandlerexceptionresolver

Some special exceptions are handled, such as the following exceptions:

Simplemappingexceptionresolver

If you want all exceptions to be handled with consent, you can use Simplemappingexceptionresolver, which maps the exception class name to the view name, that is, the exception is reported with the corresponding view when an exception occurs

       <bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >              <!-- Define the exception handling page to get the variable name of the exception information, default to exception-->              <property name= "Exceptionattribute" value= "ex" ></property >              <property name= "exceptionmappings" >                     <props>                            <!--If the following exception occurs, it maps to the error page--                            <prop key= "java.lang.ArrayIndexOutOfBoundsException" >error</prop>                     </props>              </ Property>       </bean>

  

 

Springmvc Exception Handling

Related Article

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.