spring 返回xml數據
阿新 • • 發佈:2018-06-01
div set ont details .com HR response map src
要將對象以xml數據形式返回:參考:https://blog.csdn.net/Pnoter/article/details/52218363
1:創建對象時應該填上xml註解,這樣才能將對象轉換為xml形式。
2:在對象屬性上,添加xmlElement可以將屬性解析為xml的子元素形式
例如:
對象DemoUser.java
package com.li.pojo; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="demoUser") //xml根元素demoUser public class DemoUser { private String name; private String password; public DemoUser() { } public String getName() { return name; } @XmlElement //xml子元素 name public void setName(String name) { this.name = name; } public String getPassword() {return password; } @XmlElement //xml子元素 password public void setPassword(String password) { this.password = password; } }
控制器:
DemoController.java
package com.li.controller; import com.li.pojo.DemoUser; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; @Controller public class DemoController { @RequestMapping("/demo") @ResponseBody public DemoUser demo() { DemoUser demoUser = new DemoUser(); demoUser.setName("name"); demoUser.setPassword("password"); return demoUser; } }
開啟服務,訪問:http://localhost:9002/demo
結果:
spring 返回xml數據