springboot使用requestmapping创建xml响应体接口

entity下创建类文件,类名分别为:ResponseXml,ResponseItem
controller下创建xml响应体实现方法 getResponseWithXml

---------ResponseXml Start-------
import javax.xml.bind.annotation.*;

//根标签
@XmlRootElement(name="test1")
public class ResponseXml{
private String um;
private String dept;
private int id;
private ResponseItem item;

@XmlAttribute
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}

public String getUm(){
return um;
}
public void setUm(String um){
this.um = um;
}

public String getDept(){
return dept;
}
public void setDept(String dept){
this.dept = dept;
}

public ResponseItem getItem(){
return item;
}
public void setItem(ResponseItem item){
this.item = item;
}
}
---------ResponseXml end-------

---------ResponseItem Start-------
//根标签包含的子标签
import javax.xml.bind.annotation.*;
public class ResponseItem{
private String appname;
private String appcode;

public String getAppname(){
return appname;
}
public void setAppname(String appname){
this.appname = appname;
}
public String getAppcode(){
return appcode;
}
public void setAppcode(String appcode){
this.appcode = appcode;
}
....
....
//依次类推
}

---------ResponseItem end------

---------getResponseWithXml start-------
@ResponseBody
@RequestMapping(value="/get/response/with/xml",produces={"application/xml;charset=utf-8"})
public ResponseXml getResponseWithXml(HttpServletRequest httpRequest,HttpServletResponse httpResponse,@RequestHeader MultiValueMap<String,String> reqheaders) throws IOEception{
StringBuilder buffer = new StringBuilder();
BufferedReader reader = httpRequest.getReader();
String line;
while ((line = reader.readLine())!=null){
buffer.append(line);
}
String reqBody=buffer.toString();
JSONOjbect object = JSONObject.parseObject(reqBody);
ResponseXml resXml = new ResponseXml();
ResponseItem item = new ResponseItem();
----最里层---
item.setAppname(object.getString("appname"));
item.setAppcode(object.getString("appcode"));
----最里层---

----根目录(外层)---
resXml.setUm(object.getString("um"));
resXml.setDept(object.getString("dept"));
resXml.setId(object.getInteger("id"));
resXml.setItem(item);
return resXml;

----根目录(外层)---


}


---------getResponseWithXml end-------

posted @ 2023-11-12 18:13  遇事不决,量子力学  阅读(55)  评论(0编辑  收藏  举报