Spring 3 MVC和XML示例

在Spring 3中,“ mvc:annotation-driven ”的功能之一是支持将对象转换为XML文件或从XML文件转换对象(如果JAXB在项目类路径中)。

在本教程中,我们向您展示如何将返回对象转换为XML格式,并通过Spring @MVC框架将其返回给用户。

使用的技术:

  1. Spring 3.0.5。发布
  2. JDK 1.6
  3. Eclipse 3.6
  4. Maven 3

JDK6中的JAXB
JDK6中包含JAXB,因此,您不需要手动包含JAXB库,只要使用JAXB注释对对象进行注释,Spring就会自动将其转换为XML格式。

1.项目依赖

没有额外的依赖关系,您只需要在Maven pom.xml包含Spring MVC。

<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>

	<dependencies>

		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

	</dependencies>

2.型号+ JAXB

一个简单的POJO模型,并用JAXB批注进行批注 ,随后将该对象转换为XML输出。

package com.mkyong.common.model;

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

@XmlRootElement(name = "coffee")
public class Coffee {

	String name;
	int quanlity;

	public String getName() {
		return name;
	}

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

	public int getQuanlity() {
		return quanlity;
	}

	@XmlElement
	public void setQuanlity(int quanlity) {
		this.quanlity = quanlity;
	}

	public Coffee(String name, int quanlity) {
		this.name = name;
		this.quanlity = quanlity;
	}

	public Coffee() {
	}
	
}

3.控制器

在方法返回值中添加“ @ResponseBody ”,在Spring文档中没有太多细节。

据我所知,当春天看到

  1. 用JAXB注释的对象
  2. JAXB库存在于类路径中
  3. 启用了“ mvc:annotation-driven”
  4. 用@ResponseBody注释的返回方法

它将自动处理转换。

package com.mkyong.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mkyong.common.model.Coffee;

@Controller
@RequestMapping("/coffee")
public class XMLController {

	@RequestMapping(value="{name}", method = RequestMethod.GET)
	public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {

		Coffee coffee = new Coffee(name, 100);
		
		return coffee;

	}
	
}

4. mvc:注释驱动

在您的Spring配置XML文件之一中,启用“ mvc:annotation-driven ”。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:component-scan base-package="com.mkyong.common.controller" />

	<mvc:annotation-driven />

</beans>

注意
或者,您可以声明“ spring-oxm.jar ”依赖项,并包括以下MarshallingView来处理转换。 使用此方法,您无需在方法中注释@ResponseBody

<beans ...>
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
	
	<bean id="xmlViewer" 
		class="org.springframework.web.servlet.view.xml.MarshallingView">
		<constructor-arg>
		  <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
			<property name="classesToBeBound">
				<list>
					<value>com.mkyong.common.model.Coffee</value>
				</list>
			</property>
		  </bean>
		</constructor-arg>
	</bean>
</beans>

5.演示

网址: http:// localhost:8080 / SpringMVC / rest / coffee / arabica

spring mvc and xml example demo

下载源代码

下载它– SpringMVC-XML-Example.zip (7 KB)

参考文献

  1. Spring MVC和Rss示例
  2. mvc注释驱动的JavaDoc
  3. Jaxb2Marshaller JavaDoc
  4. ResponseBody.html JavaDoc

翻译自: https://mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值