1、概览

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

1)、新增Attribute类和Error类

  1. package com.ebc.controller;
  2.  
  3. import javax.xml.bind.annotation.XmlRootElement;
  4.  
  5. @XmlRootElement
  6. public class Attribute {
  7. private String key;
  8. private String value;
  9.  
  10. public String getKey() {
  11. return key;
  12. }
  13.  
  14. public void setKey(String key) {
  15. this.key = key;
  16. }
  17.  
  18. public String getValue() {
  19. return value;
  20. }
  21.  
  22. public void setValue(String value) {
  23. this.value = value;
  24. }
  25. }
  1. package com.ebc.controller;
  2.  
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. import java.util.List;
  8.  
  9. @XmlRootElement
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. public class Error {
  12. @XmlElement(name="attribute")
  13. private List<Attribute> attributeList;
  14.  
  15. public List<Attribute> getAttributeList() {
  16. return attributeList;
  17. }
  18.  
  19. public void setAttributeList(List<Attribute> attributeList) {
  20. this.attributeList = attributeList;
  21. }
  22. }

2)、修改MyCustomErrorController类

  1. import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
  2. import org.springframework.boot.web.servlet.error.ErrorAttributes;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. /**
  14. * @author www.gomepay.com
  15. * @date 2019/11/18
  16. */
  17. @Controller
  18. public class MyCustomErrorController extends AbstractErrorController {
  19. public MyCustomErrorController(ErrorAttributes errorAttributes) {
  20. super(errorAttributes);
  21. }
  22. @RequestMapping(value = "/error", produces = MediaType.APPLICATION_XML_VALUE)
  23. @ResponseBody
  24. public Error handleError(HttpServletRequest request) {
  25. Map<String, Object> errorAttributes = super.getErrorAttributes(request, true);
  26. List<Attribute> attributes = new ArrayList<>();
  27. errorAttributes.forEach((key, value) -> {
  28. Attribute attribute = new Attribute();
  29. attribute.setKey(key);
  30. attribute.setValue(value == null ? "" : value.toString());
  31. attributes.add(attribute);
  32. });
  33. Error error = new Error();
  34. error.setAttributeList(attributes);
  35. return error;
  36. }
  37. @Override
  38. public String getErrorPath() {
  39. return "/error";
  40. }
  41. }

3、执行

1)、http://localhost:8080/

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <error>
  4. <attribute>
  5. <key>timestamp</key>
  6. <value>Wed Nov 20 17:43:59 GMT+08:00 2019</value>
  7. </attribute>
  8. <attribute>
  9. <key>status</key>
  10. <value>500</value>
  11. </attribute>
  12. <attribute>
  13. <key>error</key>
  14. <value>Internal Server Error</value>
  15. </attribute>
  16. <attribute>
  17. <key>message</key>
  18. <value>test exception</value>
  19. </attribute>
  20. <attribute>
  21. <key>trace</key>
  22. <value>java.lang.RuntimeException: test exception at com.ebc.controller.MyController.handler(MyController.java:15)。。。</value>
  23. </attribute>
  24. <attribute>
  25. <key>path</key>
  26. <value>/</value>
  27. </attribute>
  28. </error>

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <error>
  4. <attribute>
  5. <key>timestamp</key>
  6. <value>Wed Nov 20 17:49:51 GMT+08:00 2019</value>
  7. </attribute>
  8. <attribute>
  9. <key>status</key>
  10. <value>404</value>
  11. </attribute>
  12. <attribute>
  13. <key>error</key>
  14. <value>Not Found</value>
  15. </attribute>
  16. <attribute>
  17. <key>message</key>
  18. <value>No message available</value>
  19. </attribute>
  20. <attribute>
  21. <key>path</key>
  22. <value>/other</value>
  23. </attribute>
  24. </error>

springboot - 返回xml error 从自定义的 ErrorController的更多相关文章

  1. springboot - 返回JSON error 从自定义的 ErrorController

    使用AbstractErrorController(是ErrorController的实现),返回json error. 1.概览 2.基于<springboot - 映射 /error 到自定 ...

  2. springboot - 使用ErrorAttributes 在我们自定义的 ErrorController中

    1.概览 基于<springboot - 映射 /error 到自定义且实现了ErrorController的Controller>改造,仅将MyCustomErrorController ...

  3. springboot实现xml传参和返回值

    1.新建maven工程xml-bean-convert pom.xml如下 <?xml version="1.0" encoding="UTF-8"?&g ...

  4. Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据

    我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...

  5. SpringBoot返回JSON

    目录 1.SpringBoot返回JSON简介 2.整合jackson-databind 3.整合Gson 4.整合fastjson 1.SpringBoot返回JSON简介 随着web开发前后端分离 ...

  6. springboot返回统一接口与统一异常处理

    springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...

  7. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  8. mvc 返回 xml

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  9. Spring Boot 返回 XML 数据,一分钟搞定!

    Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击查看之前分享的 Spring Boot 返回 JSON 数据,一分钟搞定! ...

随机推荐

  1. A*算法和K短路(A*)

    堪称最好的A算法 https://blog.csdn.net/b2b160/article/details/4057781 K短路(A) https://www.jianshu.com/p/27019 ...

  2. Java后端 带File文件及其它参数的Post请求

    http://www.roak.com Java 带File文件及其它参数的Post请求 对于文件上传,客户端通常就是页面,在前端web页面里实现上传文件不是什么难事,写个form,加上enctype ...

  3. unique 验证 criteria 使用

    model array('code', 'unique', 'criteria' =>array('condition' =>'schoolid=:schoolid','params' = ...

  4. pytorch深度学习神经网络实现手写字体识别

    利用平pytorch搭建简单的神经网络实现minist手写字体的识别,采用三层线性函数迭代运算,使得其具备一定的非线性转化与运算能力,其数学原理如下: 其具体实现代码如下所示:import torch ...

  5. 使 Firefox 和 Vivaldi 只在新标签页显示书签栏

    Firefox 新建 ~/.mozilla/firefox/rre9emvh.default/chrome/userChrome.css (大概不同人的 rre9emvh.default 目录会有不同 ...

  6. 一 SpringMvc概述&入门配置

    SpringMVC: 类似Struts2的MVC框架,属于SpringFrameWork的后续产品. 与Struts2的区别: 参数传递:  Struts2通过模型驱动,属性设置set方法,值栈.类级 ...

  7. 【剑指Offer面试编程题】题目1369:字符串的排列--九度OJ

    题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...

  8. jqGrid 多选复选框 编辑列 方法事件

    参考:https://blog.csdn.net/zsq520520/article/details/53375284?locationNum=8&fps=1

  9. JAVA 数据库操作工具类----sqllite

    package com.asc.db; import android.content.ContentValues; import android.content.Context; import and ...

  10. Django:验证码相关问题

    http://blog.csdn.net/csapr1987/article/details/7728315 https://zhidao.baidu.com/question/13837387222 ...