springboot - 返回xml error 从自定义的 ErrorController
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>
springboot - 返回xml error 从自定义的 ErrorController的更多相关文章
- springboot - 返回JSON error 从自定义的 ErrorController
使用AbstractErrorController(是ErrorController的实现),返回json error. 1.概览 2.基于<springboot - 映射 /error 到自定 ...
- springboot - 使用ErrorAttributes 在我们自定义的 ErrorController中
1.概览 基于<springboot - 映射 /error 到自定义且实现了ErrorController的Controller>改造,仅将MyCustomErrorController ...
- springboot实现xml传参和返回值
1.新建maven工程xml-bean-convert pom.xml如下 <?xml version="1.0" encoding="UTF-8"?&g ...
- Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...
- SpringBoot返回JSON
目录 1.SpringBoot返回JSON简介 2.整合jackson-databind 3.整合Gson 4.整合fastjson 1.SpringBoot返回JSON简介 随着web开发前后端分离 ...
- springboot返回统一接口与统一异常处理
springboot返回统一接口与统一异常处理 编写人员:yls 编写时间:2019-9-19 0001-springboot返回统一接口与统一异常处理 简介 创建统一的返回格式 Result 封装统 ...
- Android XML中引用自定义内部类view的四个why
今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...
- mvc 返回 xml
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- Spring Boot 返回 XML 数据,一分钟搞定!
Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击查看之前分享的 Spring Boot 返回 JSON 数据,一分钟搞定! ...
随机推荐
- python学习 —— seaborn、matplotlib、pandas、numpy package的混合使用
这里使用了Titanic Machine learning数据集,然后通过Seaborn的函数来拟合和绘制回归线,matplotlib进行可视化. 先来一个简单的测试: import pandas a ...
- pdf.js-----后端返回utf-8数据流,前端处理数据展示pdf
需求:做项目联调接口时,发现知识库展示pdf未果,经与后端人员沟通,发现以下问题: 1.接口返回的是utf-8数据流,但是前端调用的是base64解析方法: 导致功能有误: 方案一:将后端返回的utf ...
- 「国家集训队」小Z的袜子
「国家集训队」小Z的袜子 传送门 莫队板子题. 注意计算答案的时候,由于分子分母都要除以2,所以可以直接约掉,这样在开桶算的时候也方便一些. 参考代码: #include <algorithm& ...
- nginx_1_初始nginx
一.nginx简介: nginx是一个性能优秀的web服务器,同时还提供反向代理,负载均衡,邮件代理等功能.是俄罗斯人用C语言开发的开源软件. 二.安装nginx step1:安装依赖库 pcre(支 ...
- java连接sql server 2008
请先确保已经设置好了sa,如果不是,可以参照下面链接修改http://jingyan.baidu.com/article/8cdccae9452b3c315513cd52.html 然后重启数据库,重 ...
- 使用JNA替代JNI调用本地方法
JNA全称是Java Native Access,是Sun推出的一种调用本地方法技术,比起它的同门师兄JNI,JNA大大简化了调用本地方法的过程,使用也比较方便, JNA是在JNI的基础上完善的,用青 ...
- PaperReading20200221
CanChen ggchen@mail.ustc.edu.cn Busy... Human-level concept learning through probabilistic program i ...
- linux系统下安装两个或多个tomcat(转)
tomcat不用添加到环境变量中 直接解压两个tomcat 来到第二个tomcat的conf目录下 打开server.xml更改端口: 修改server.xml配置和第一个不同的启动.关闭监听端口 ...
- 谁说5G网络无敌?第六代Wi-Fi表示不服
导读 随着第五代移动通信技术(5G)正式商用,同属第五代的Wi-Fi技术(802.11ac)的处境就非常尴尬了,除了不存流量费用外,无论是网速.设备连接数还是网络延迟,5G都拥有秒杀802.11ac的 ...
- 微信二次分享的JSSDK的调用
网页端微信的二次分享如果不调用分享的SDK,分享之后就不会带有标题.描述 .缩略图 微信分享SDK调用 引入 <script src="//res.wx.qq.com/open/js/ ...