springboot 기본 예외 처리
                                            
 5788 단어  spring-bootspring-cloud
                    
{
    "timestamp": 1517294278132,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "com.lgy.common.exception.BusinessException",
    "message": "[001] uncheck  !",
    "path": "/validateExceptionTest"
}//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.web;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeStacktrace;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class BasicErrorController extends AbstractErrorController {
    private final ErrorProperties errorProperties;
    public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
        this(errorAttributes, errorProperties, Collections.emptyList());
    }
    public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) {
        super(errorAttributes, errorViewResolvers);
        Assert.notNull(errorProperties, "ErrorProperties must not be null");
        this.errorProperties = errorProperties;
    }
    public String getErrorPath() {
        return this.errorProperties.getPath();
    }
    @RequestMapping(
        produces = {"text/html"}
    )
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        HttpStatus status = this.getStatus(request);
        Map model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML)));
        response.setStatus(status.value());
        ModelAndView modelAndView = this.resolveErrorView(request, response, status, model);
        return modelAndView == null?new ModelAndView("error", model):modelAndView;
    }
    @RequestMapping
    @ResponseBody
    public ResponseEntity  SpringBoot의 기본 비정상 처리 방식을 대체하려면 ErrorController를 상속합니다.
package com.lgy.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
 * Created by fengch on 2018/1/30.
 */
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class FundaErrorController  implements ErrorController {
    private static final String PATH = "/error";
    @Autowired
    private ErrorAttributes errorAttributes;
    @Override
    public String getErrorPath() {
        return PATH;
    }
    @RequestMapping
    @ResponseBody
    public JSONObject doHandleError(HttpServletRequest request) {
        RequestAttributes requestAttributes = new ServletRequestAttributes(request);
        Map errorAttributesData = errorAttributes.getErrorAttributes(requestAttributes,true);
        Integer status=(Integer)errorAttributesData.get("status");  // 
        String path=(String)errorAttributesData.get("path");        // 
        String messageFound=(String)errorAttributesData.get("message");   // 
        JSONObject reData = new JSONObject();
        reData.put("status_", status);
        reData.put("path_", path);
        reData.put("message", messageFound);
        return reData;
    }
}
 다음으로 액세스하여 사용자 지정 처리된 예외 정보입니다.
{
    "message": "[001] uncheck  !",
    "path_": "/validateExceptionTest",
    "status_": 500
}이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Keycloak이 Active Directory에 등록된 사용자로 인증할 수 있도록 합니다.사내 시스템을 출시함에 있어서, 전회사에서는 Web시스템마다 로그인하고 있어 혐오가 있었으므로, 꼭 싱글 사인온으로 하고 싶다고 생각했다. 그 실현에, 옛날 조금만 평가한 OpenAM라든지의 정보를 구구어 낚시하기 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.