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 数据,一分钟搞定! ...
随机推荐
- A*算法和K短路(A*)
堪称最好的A算法 https://blog.csdn.net/b2b160/article/details/4057781 K短路(A) https://www.jianshu.com/p/27019 ...
- Java后端 带File文件及其它参数的Post请求
http://www.roak.com Java 带File文件及其它参数的Post请求 对于文件上传,客户端通常就是页面,在前端web页面里实现上传文件不是什么难事,写个form,加上enctype ...
- unique 验证 criteria 使用
model array('code', 'unique', 'criteria' =>array('condition' =>'schoolid=:schoolid','params' = ...
- pytorch深度学习神经网络实现手写字体识别
利用平pytorch搭建简单的神经网络实现minist手写字体的识别,采用三层线性函数迭代运算,使得其具备一定的非线性转化与运算能力,其数学原理如下: 其具体实现代码如下所示:import torch ...
- 使 Firefox 和 Vivaldi 只在新标签页显示书签栏
Firefox 新建 ~/.mozilla/firefox/rre9emvh.default/chrome/userChrome.css (大概不同人的 rre9emvh.default 目录会有不同 ...
- 一 SpringMvc概述&入门配置
SpringMVC: 类似Struts2的MVC框架,属于SpringFrameWork的后续产品. 与Struts2的区别: 参数传递: Struts2通过模型驱动,属性设置set方法,值栈.类级 ...
- 【剑指Offer面试编程题】题目1369:字符串的排列--九度OJ
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...
- jqGrid 多选复选框 编辑列 方法事件
参考:https://blog.csdn.net/zsq520520/article/details/53375284?locationNum=8&fps=1
- JAVA 数据库操作工具类----sqllite
package com.asc.db; import android.content.ContentValues; import android.content.Context; import and ...
- Django:验证码相关问题
http://blog.csdn.net/csapr1987/article/details/7728315 https://zhidao.baidu.com/question/13837387222 ...