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、执行
<?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>