样例
控制器:
@RequestMapping(path = "XMLTest")public Map XMLTest() {Map<String, Object> map = new HashMap<>();map.put("bossAge", 1);map.put("employeeAge", 2);map.put("paramsA", 3);map.put("paramsB", 4);return map;}
引入 xml 依赖
<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></dependency>
开启参数式内容协商功能(可选)
开启后,在请求中携带的 format 参数如果为 xml 或 json ,Spring Boot 就会返回对应类型的响应格式了。
spring:contentnegotiation:favor-parameter: true #开启请求参数内容协商模式
返回 json 和 xml
只需要改变请求头中Accept字段。Http协议中规定的,告诉服务器本客户端可以接收的数据类型。

