spring MVC中@ResponseBody返回XML格式的数据

本文介绍了一种XML数据格式及其对应的数据模型实现,并通过Spring MVC框架展示了如何将Java对象转换为XML格式返回。具体包括了HotCar实体类及Info类的设计,以及Controller中用于返回XML数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求的xml数据格式:

<info detail="" error="0" timeservice="8ms" type="hot_car_details">
<hot_car carReferPrice="40.04" carYear="2017" gearBox="8挡手自一体" id="121280" name="40TFSI 进取型" power="230马力" price="30.83"/>
</info>

建立数据模型:

package com.fh.entity;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "info")
public class Info {

	HotCar hot_car;

	String type;
	String error;
	String timeservice;
	String detail;

	public HotCar getHot_car() {
		return hot_car;
	}

	@XmlElement(name = "hot_car")
	public void setHot_car(HotCar hot_car) {
		this.hot_car = hot_car;
	}

	public String getType() {
		return type;
	}

	@XmlAttribute
	public void setType(String type) {
		this.type = type;
	}

	public String getError() {
		return error;
	}

	@XmlAttribute
	public void setError(String error) {
		this.error = error;
	}

	public String getTimeservice() {
		return timeservice;
	}

	@XmlAttribute
	public void setTimeservice(String timeservice) {
		this.timeservice = timeservice;
	}

	public String getDetail() {
		return detail;
	}

	@XmlAttribute
	public void setDetail(String detail) {
		this.detail = detail;
	}

}

package com.fh.entity;

import javax.xml.bind.annotation.XmlAttribute;

public class HotCar {
	
	String id;
	String name;
	String power;
	String carYear;
	String gearBox;
	String price;
	String carReferPrice;

	public String getId() {
		return id;
	}

	@XmlAttribute
	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	@XmlAttribute
	public void setName(String name) {
		this.name = name;
	}

	public String getPower() {
		return power;
	}

	@XmlAttribute
	public void setPower(String power) {
		this.power = power;
	}

	public String getCarYear() {
		return carYear;
	}

	@XmlAttribute
	public void setCarYear(String carYear) {
		this.carYear = carYear;
	}

	public String getGearBox() {
		return gearBox;
	}

	@XmlAttribute
	public void setGearBox(String gearBox) {
		this.gearBox = gearBox;
	}

	public String getPrice() {
		return price;
	}

	@XmlAttribute
	public void setPrice(String price) {
		this.price = price;
	}

	public String getCarReferPrice() {
		return carReferPrice;
	}

	@XmlAttribute
	public void setCarReferPrice(String carReferPrice) {
		this.carReferPrice = carReferPrice;
	}

}

控制器里返回xml的代码:

	@RequestMapping(value = "/hotCar")
	public @ResponseBody Info hotCar(){
		
		Info info = new Info();
        info.setType("hot_car_details");
        info.setError("0");
        info.setTimeservice("8ms");
        info.setDetail("");
        
        HotCar hotCar = new HotCar();
        hotCar.setId("121280");
        hotCar.setName("40TFSI 进取型");
        hotCar.setPower("230马力");
        hotCar.setCarYear("2017");
        hotCar.setGearBox("8挡手自一体");
        hotCar.setPrice("30.83");
        hotCar.setCarReferPrice("40.04");
        
        info.setHot_car(hotCar);

		return info;
	}

转载于:https://my.oschina.net/hehongbo/blog/761362

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值