springmvc接收xml格式数据,发送xml格式数据。

1.类封装对应的xml数据。添加注解@XmlRootElement,标识封装xml数据,否则为json数据。类上再添加@XmlAccessorType(XmlAccessType.FIELD)代表@XmlElement注解加在字段上。

2.属性上添加@XmlElement对应xml标签名@XmlAttribute对应某标签的属性。

发送xml请求消息体:


	yaoct
	123456

对应响应消息体封装类:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
	@XmlElement
	String name;
	@XmlElement
	String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

Controller:

@RequestMapping("/demo")
@ResponseBody
public MessageAccept demo(@RequestBody User user){
	System.out.println(user.getName());
	System.out.println(user.getPassword());
	return new MessageAccept("ada", "sq", "eqd");
}

响应消息体对应封装对象:(需要无参构造函数,这里省略了一些注解最好配置同上)

@XmlRootElement
public class MessageAccept {
	String name;
	String num;
	String lala;
	public MessageAccept(){}
	public MessageAccept(String name, String num, String lala) {
		super();
		this.name = name;
		this.num = num;
		this.lala = lala;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getLala() {
		return lala;
	}
	public void setLala(String lala) {
		this.lala = lala;
	}
	
}

响应xml



    eqd
    ada
    sq

 

 

特别注意,这些注解添加maven包(版本太新会不兼容spring):


            javax.xml.bind
            jaxb-api
            2.1
 

参考:

blog.csdn.net/gaoqiao1988/article/details/50481578

blog.csdn.net/mytt_10566/article/details/68947377

blog.csdn.net/Pnoter/article/details/52218363

你可能感兴趣的:(典型demo)