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>

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. ZeroTier 局域网组建工具

    无公网IP通过ZeroTier实现内网穿透 需求:想要在公司访问家里内网NAS,或是在家里访问公司服务 有固定的公网IP或动态的公网IP:常见的方案动态域名解析做端口转发方式等 无公网IP:常见的实现 ...

  2. css样式和定义的class都没问题,但样式却没生效

    今天开发遇到过这样的问题,主要原因是 css 文件格式有问题导致的.有问题的 css 样式的那一行下面的 css 样式不能生效

  3. 树莓派4B踩坑指南 - (1)系统简介及特性

    系统简介及特性 19年双十一入坑树莓派4B,发现不是一般的坑,对于新出来的4B,从外包装壳,到接口,到内核,很多老的资料已经不再适用,又没有什么特别大的论坛可以讨论,只能自己一点点的摸索. 所以将遇到 ...

  4. Windows一键启动多个软件

    @echo off title 启动工作环境 @echo 正在启动FileZilla%start+空格+/d+空格+目录路径+空格+程序名 % start /d"F:\安装包\FileZil ...

  5. PHP+Mysql实现网站顶和踩投票功能实例

    PHP+Mysql实现网站顶和踩投票功能实例,通过记录用户IP,判断用户的投票行为是否有效,该实例也可以扩展到投票系统中. 首先我们在页面上放置“顶”和“踩”的按钮,即#dig_up和#dig_dow ...

  6. 使用oracle 的 PL/Sql 定时执行一个存储过程

    CSDN日报20170322--<关于软件研发的一些体会总结> 同步博客至 CSDN ,让更多开发者看到你的文章 看微博技术大咖解析互联网应用架构实战 使用oracle 的 PL/Sql ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:复选框(Checkbox)和单选框(Radio)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. INUX下抓取当前登录用户登录密码的工具:mimipenguin

    前有Mimikatz,今有mimipenguin,近日国外安全研究员huntergregal发布了工具mimipenguin,一款Linux下的密码抓取神器,可以说弥补了Linux下密码抓取的空缺. ...

  9. Keras入门——(2)卷积神经网络CNN

    前期准备工作参考:https://www.cnblogs.com/ratels/p/11144881.html 基于CNN算法利用Keras框架编写代码实现对Minst数据分类识别: from ker ...

  10. Linux centosVMware zip压缩工具、tar打包、打包并压缩

    一. zip压缩工具 可以用来压缩文件和目录,压缩目录是需要指定目录下的文件. [root@davery tmp]# cp 1.txt davery/[root@davery tmp]# du -sh ...