spring mvc global error handling problems encountered

Party calls the project the project today interfaces report an error directly display exception information page 415 (server can not handle media format that came with the request). Very beautiful. 

This is obviously not my problem code, the caller is not requested as agreed. So think the whole global exception handler. This project uses restful spring mvc provide external interfaces. 

 Internet to find the next, there are basically two solutions:

1. inherited HandlerExceptionResolver

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component("handlerExceptionResolver")
public class GlobalExceptionExceptionHandler implements HandlerExceptionResolver {

    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionExceptionHandler.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        logger.error("GlobalExceptionExcetionHandler error:"+ request.getRequestURL(),ex);
        ModelAndView mv = new ModelAndView();
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        mv.setView(view);
        mv.addObject("status", "fail");
        mv.addObject("message", "system error");
        return mv;
    }
}

2. Use @ExceptionHandler in the controller, but this is only valid for the current controller request

@ResponseBody
    @ExceptionHandler(Exception.class)
    public String bindException(Exception e) {
       //...
        return result;
    }

Then select the first embodiment, the test

When the spring mvc packets always able to enter a null pointer controller method, but 415 is not useful. 

Internet looking for a long time that is 415, 404 is such error, a null pointer Exception .. this will be a look at the difference

The solution is to configure the error-page in the web.xml 

<! - There are a number of questions at the beginning of 4, are exhaustive, how do -> 
< error-Page > 
    < error-code > 400 </ error-code > 
    < LOCATION > ? / Error / code = 400 </ LOCATION > 
  </ error-Page > 
  < error-Page > 
    < error-code > 403 </ error-code > 
    < LOCATION > / error /? code = 403 </ LOCATION > 
  </ error-Page > 
  < error-Page > 
    <error-code>404</error-code>
    <location>/error/?code=404</location>
  </error-page>
  <error-page>
    <error-code>415</error-code>
    <location>/error/?code=415</location>

Then / error / controller corresponding 

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/error")
public class ErrorController {
    @RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String error_404(String code) {
        JSONObject o = new JSONObject();
        o.put("status","error");
        o.put("message",code);
        return o.toJSONString();
    }
}

Finally: error and exception difference?

1. error jvm operating state is generally caused by a problem such as a NoClassDefFoundError (based packaging due to leakage), StackOverflowError

, Exception run time can be expected, a ClassNotFoundException (reflection, caused by dynamic loading class not found)

Error (error) indicates an abnormal system-level error and the program does not have to deal with is an internal error or a hardware problem java runtime environment. For example: lack of memory and other resources. For this error, the program basically powerless, in addition to no choice but out of operation, it is thrown by the Java virtual machine.

Exception (violation) expressed the need to capture or exception handling procedures required, it deals with general issues caused problems because of defects caused by programming or external input, is a program that must be addressed

 

The server can not handle the media that came with the requested format

Guess you like

Origin www.cnblogs.com/zhangchenglzhao/p/11354561.html