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. nodejs中this详解

    最近在用Nodejs进行APP运维服务管理系统开发时发现,nodejs中的this经常会变,查了下资料后发现this在不同的代码位置中代表不同的涵义,在实际运用过程中可以用var self = thi ...

  2. 「APIO2012」派遣

    「APIO2012」派遣 传送门 当预算超过限制时,优先丢掉薪水高的忍者(左偏树维护一下),然后答案取合法答案的最大值. 参考代码: #include <algorithm> #inclu ...

  3. Python 基础之函数初识与函数参数

    一.函数初识 定义:满足某一个方法 满足某一个功能#(1)功能(包裹一部分代码 实现某一个功能 达成某一个目的)#(2)可以反复调用,提高代码的复用性,提高开发效率,便于维护管理#(3)函数的基本格式 ...

  4. Python 中的else

    在其他程序语言中,else 似乎只是与 if 关键字有缘分.而与其他的关键字没有联系,不能搭配使用,而在python中,else 除了与 if 匹配外, 还可以与for.while/ try等关键字匹 ...

  5. openstack的一台Nova主机上的虚拟机网络的配置

    1.一台虚拟机器的网络配置,通过openstack/nova计算节点服务生成的虚拟机配置文件 <interface type='bridge'> <mac address='fa:1 ...

  6. 哈希表,Java中的hashCode

    哈希表: 将我们所需的键通过哈希函数转换索引,然后存储在一个数组中. 哈希表是时间和空间之间的平衡,体现空间换时间的算法思想(联想到预加载,缓存等,有时候多存储,预处理缓存一些东西,带来时间复杂度的改 ...

  7. js去后台传递的值

    function test(){ var param = [[${list}]];//以集合为例 } 如果list里面是实体类那么就需要重写toString,或者转为json

  8. 如何给Sqlite添加复合主键

    如果是想两个字段组成一个复合主键的话可以如下.SQL code sqlite> create table t2 ( ...> id1 int , ...> id2 int, ...& ...

  9. Day2-F-A Knight's Journey POJ-2488

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  10. PHP+MySQL实现在线测试答题实例

    这个实例主要给大家介绍如何使用jQuery+PHP+MySQL来实现在线测试题,包括动态读取题目,答题完毕后台评分,并返回答题结果. 读取答题列表: $sql = "select * fro ...