zoukankan      html  css  js  c++  java
  • springboot

    1、概览

    2、在《springboot - 返回JSON error 从自定义的 ErrorController》基础上,做如下调整:

    1)、新增Attribute类和Error类

    package com.ebc.controller;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Attribute {
        private String key;
        private String value;
    
        public String getKey() {
            return key;
        }
    
        public void setKey(String key) {
            this.key = key;
        }
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    }
    package com.ebc.controller;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import java.util.List;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Error {
        @XmlElement(name="attribute")
        private List<Attribute> attributeList;
    
        public List<Attribute> getAttributeList() {
            return attributeList;
        }
    
        public void setAttributeList(List<Attribute> attributeList) {
            this.attributeList = attributeList;
        }
    }

    2)、修改MyCustomErrorController类

    import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
    import org.springframework.boot.web.servlet.error.ErrorAttributes;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author www.gomepay.com
     * @date 2019/11/18
     */
    @Controller
    public class MyCustomErrorController extends AbstractErrorController {
        public MyCustomErrorController(ErrorAttributes errorAttributes) {
            super(errorAttributes);
        }
        @RequestMapping(value = "/error", produces = MediaType.APPLICATION_XML_VALUE)
        @ResponseBody
        public Error handleError(HttpServletRequest request) {
            Map<String, Object> errorAttributes = super.getErrorAttributes(request, true);
    
            List<Attribute> attributes = new ArrayList<>();
            errorAttributes.forEach((key, value) -> {
                Attribute attribute = new Attribute();
                attribute.setKey(key);
                attribute.setValue(value == null ? "" : value.toString());
                attributes.add(attribute);
            });
            Error error = new Error();
            error.setAttributeList(attributes);
            return error;
        }
        @Override
        public String getErrorPath() {
            return "/error";
        }
    }

    3、执行

    1)、http://localhost:8080/

    <?xml version="1.0" encoding="utf-8"?>
    
    <error>
      <attribute>
        <key>timestamp</key>
        <value>Wed Nov 20 17:43:59 GMT+08:00 2019</value>
      </attribute>
      <attribute>
        <key>status</key>
        <value>500</value>
      </attribute>
      <attribute>
        <key>error</key>
        <value>Internal Server Error</value>
      </attribute>
      <attribute>
        <key>message</key>
        <value>test exception</value>
      </attribute>
      <attribute>
        <key>trace</key>
        <value>java.lang.RuntimeException: test exception at com.ebc.controller.MyController.handler(MyController.java:15)。。。</value>
      </attribute>
      <attribute>
        <key>path</key>
        <value>/</value>
      </attribute>
    </error>

    2)、http://localhost:8080/other

    <?xml version="1.0" encoding="utf-8"?>
    
    <error>
      <attribute>
        <key>timestamp</key>
        <value>Wed Nov 20 17:49:51 GMT+08:00 2019</value>
      </attribute>
      <attribute>
        <key>status</key>
        <value>404</value>
      </attribute>
      <attribute>
        <key>error</key>
        <value>Not Found</value>
      </attribute>
      <attribute>
        <key>message</key>
        <value>No message available</value>
      </attribute>
      <attribute>
        <key>path</key>
        <value>/other</value>
      </attribute>
    </error>

  • 相关阅读:
    【WPF】自定义CheckBox复选框
    如何在WPF中引用Windows.System.Forms.Integration
    【转载】wpf DataGrid自动显示行号
    C#网络编程(订立协议和发送文件) Part.4
    C#网络编程(接收文件) Part.5
    状态模式
    C#网络编程(同步传输字符串) Part.2
    我的一个自己写的更新缓存的aop实例
    C#网络编程(基本概念和操作) Part.1
    mssql根据分类查询每个分类前100条数据 表名m_data,分类字段名m_type
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/11899429.html
Copyright © 2011-2022 走看看