Spring 3 MVCとXMLの例

Spring 3 MVCおよびXMLの例

Spring 3では、「http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-annotation-driven[mvc:annotation-駆動]”は、JAXBがプロジェクトクラスパスにある場合、XMLファイルとの間でオブジェクトを変換するためのサポートです。

このチュートリアルでは、戻りオブジェクトをXML形式に変換し、Spring @MVCフレームワークを介してユーザーに返す方法を示します。

使用される技術:

  1. Spring 3.0.5.RELEASE

  2. JDK 1.6

  3. Eclipse 3.6

  4. メーベン3

JAXB in JDK6
JAXBはJDK6に含まれているため、オブジェクトにJAXB注釈が付けられている限り、JAXBライブラリを手動で含める必要はありません。SpringはそれをXML形式に自動的に変換します。

1. プロジェクトの依存関係

追加の依存関係はありません。Mavenpom.xmlにのみSpringMVCを含める必要があります。

    
        3.0.5.RELEASE
    

    

        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-web
            ${spring.version}
        

        
            org.springframework
            spring-webmvc
            ${spring.version}
        

    

2. モデル+ JAXB

単純なPOJOモデルで、JAXB annotationで注釈が付けられ、後でこのオブジェクトをXML出力に変換します。

package com.example.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 documentationには詳細を追加しません。

私が知っているように、春を見ると

  1. JAXBアノテーションが付けられたオブジェクト

  2. JAXBライブラリがクラスパスに存在しました

  3. 「mvc:annotation-driven」が有効になっています

  4. @ResponseBodyアノテーションが付けられた戻りメソッド

変換は自動的に処理されます。

package com.example.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.example.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:annotation-driven

Spring構成XMLファイルの1つで、「mvc:annotation-driven」を有効にします。



    

    

Note
または、「spring-oxm.jar」依存関係を宣言し、次のMarshallingViewを含めて、変換を処理することもできます。 このメソッドでは、メソッドに@ResponseBodyに注釈を付ける必要はありません。


    

    
        
          
            
                
                    com.example.common.model.Coffee
                
            
          
        
    

ソースコードをダウンロード

ダウンロード–SpringMVC-XML-Example.zip(7 KB)