• spring mvc: xml练习


    xml练习,得到的结果是:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xml>
    <code>200</code>
    <message>success</message>
    <user>
        <name>李四</name>
        <id>2</id>
    </user>
    </xml>
    

      

    需要用的的注解:@ResponseBody

    org.springframework.web.bind.annotation.ResponseBody;

    将内容输出为xml,json格式(非html格式)

    其他注解:

    javax.xml.bind.annotation.XmlElement;
    javax.xml.bind.annotation.XmlElements;
    javax.xml.bind.annotation.XmlRootElement;
    javax.xml.bind.annotation.XmlAccessorType;
    javax.xml.bind.annotation.XmlAccessType;

    包名:json

    user.java

    package json;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAccessType;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.NONE)
    public class User {
    
    	@XmlElement
    	String name;
    	@XmlElement
    	Integer id;	
    	
    	public User(String name, Integer id) {		
    		this.name = name;
    		this.id = id;
    	}
    	
    	
    	public User() {	
    	}
    
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public Integer getId() {
    		return id;
    	}
    	
    	public void setId(Integer id) {
    		this.id = id;
    	}	
    	
    }
    

      

    BaseXmlResult.java根信息

    package json;
    
    public class BaseXmlResult {
    
    	protected String code;
    	protected String message;
    	
    	
    	public String getCode() {
    		return code;
    	}
    	public void setCode(String code) {
    		this.code = code;
    	}
    	public String getMessage() {
    		return message;
    	}
    	public void setMessage(String message) {
    		this.message = message;
    	}		
    	
    }
    

      

    UserActiveResult.java User列表信息

    package json;
    
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElements;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAccessType;
    
    
    @XmlRootElement(name="xml")
    @XmlAccessorType(XmlAccessType.NONE)
    public class UserActiveResult<T> extends BaseXmlResult {	
    	
    	@XmlElements({
    		@XmlElement(name="user", type=User.class)
    	})		
    	private T data;
    
    	public T getData() {
    		return data;
    	}
    	public void setData(T data) {
    		this.data = data;
    	}
    	
    	public String getCode() {
    		return code;
    	}
    	public void setCode(String code) {
    		this.code = code;
    	}
    	public String getMessage() {
    		return message;
    	}
    	public void setMessage(String message) {
    		this.message = message;
    	}
    	
    }
    

      

    再来看看UserController.java

    package json;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping(value="/user")
    public class UserController {
    
    	@RequestMapping(value="/list", method=RequestMethod.GET)	
    	@ResponseBody 
    	public UserActiveResult getUser()
    	{
    		UserActiveResult<User> list = new UserActiveResult<User>();
    		list.setCode("200");
    		list.setMessage("success");
    		
    		User user = new User();
    		user.setId(1);
    		user.setName("张三");
    		
    		list.setData(user);	
    		
    		return list;
    		
    	}
    }
    

      

  • 相关阅读:
    【JZOJ4876】【NOIP2016提高A组集训第10场11.8】基因突变
    【JZOJ4869】【NOIP2016提高A组集训第9场11.7】平均数
    【JZOJ4868】【NOIP2016提高A组集训第9场11.7】Simple
    【JZOJ4861】【NOIP2016提高A组集训第7场11.4】推冰块
    【JZOJ4860】【NOIP2016提高A组集训第7场11.4】分解数
    【JZOJ4859】【NOIP2016提高A组集训第7场11.4】连锁店
    【JZOJ4359】【GDKOI2016】魔卡少女
    【JZOJ4848】【GDOI2017模拟11.3】永恒的契约
    【JZOJ4855】【NOIP2016提高A组集训第6场11.3】荷花池塘
    Java编程的逻辑 (16)
  • 原文地址:https://www.cnblogs.com/achengmu/p/9049632.html
Copyright © 2020-2023  润新知