@ResponseBody返回一个xml格式的数据

1.前端js请求:

重点:dataType:"xml"

 $("#json5").click(function(){
		 	$.ajax({
			type:"Post",//请求类型
			url:"/mvc-demo/user/nameProving5?t="+new Date().getTime(),//请求的url
			data:{name:$(".name").val()},//请求参数
			dataType:"xml",//ajax接口(请求url)返回的数据类型
			success:function(data){//data:返回数据(json对象)
				$(".name5").val(data);
				/* $(".errorFont").text(data);
				$(".errorFont").css("color","red"); */
			
			},
			error:function(data){
				$(".errorFont").text("发生未知错误,请联系管理员5!");
				$(".errorFont").css("color","red");
			}
		});
		});

2、后端代码处理

两个实体类:

package com.mvc.demo.pojo;

public class Userss {
	private String name;
	private String pwd;
	private String realName;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public Userss(String name, String pwd) {
		super();
		this.name = name;
		this.pwd = pwd;
	}
	
	public Userss(String name, String pwd, String realName) {
		super();
		this.name = name;
		this.pwd = pwd;
		this.realName = realName;
	}
	public Userss(){};
}

继承的类,有XML注解:

package com.mvc.demo.pojo;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class UserX extends Userss {
	public UserX(){}
	public UserX(String name, String pwd, String realName){
		super(name,pwd,realName);
	}
}

3、后端逻辑处理返回

//返回一个xml
	@RequestMapping("/nameProving5")	
	@ResponseBody
	public Userss nameProving5(String name){
		Userss user=userService.getUserByName(name);
		UserX u=new UserX(user.getName(),user.getPwd(),user.getRealName());
		return u;
	}

 

你可能感兴趣的:(Spring)