Spring 3 exemple MVC et JSON

Exemple Spring 3 MVC et JSON

Dans ce didacticiel, nous vous montrons comment générer des données JSON dans le framework Spring MVC.

Technologies utilisées:

  1. Spring 3.2.2.RELEASE

  2. Jackson 1.9.10

  3. JDK 1.6

  4. Eclipse 3.6

  5. Maven 3

_P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath.
_

1. Dépendances du projet

Obtenez les dépendances Jackson et Spring.

pom.xml


    4.0.0
    com.example.common
    SpringMVC
    war
    1.0-SNAPSHOT
    SpringMVC Json Webapp
    http://maven.apache.org

    
        3.2.2.RELEASE
        1.9.10
        1.6
    

    

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

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

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

        
        
            org.codehaus.jackson
            jackson-mapper-asl
            ${jackson.version}
        

    

    
        SpringMVC
        
          
            org.apache.maven.plugins
            maven-eclipse-plugin
            2.9
            
                true
                false
                2.0
            
          
          
            org.apache.maven.plugins
            maven-compiler-plugin
            2.3.2
            
                ${jdk.version}
                ${jdk.version}
            
          
        
    

2. Modèle

Un simple POJO, affiche plus tard cet objet sous forme de données JSON formatées.

package com.example.common.model;

public class Shop {

    String name;
    String staffName[];

    //getter and setter methods

}

3. Manette

Ajoutez@ResponseBody comme valeur de retour. Wen Spring voit

  1. La bibliothèque Jackson existe dans le chemin de classe du projet

  2. Lemvc:annotation-driven est activé

  3. Méthode de retour annotée avec @ResponseBody

Spring gérera automatiquement la conversion JSON.

JSONController.java

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.Shop;

@Controller
@RequestMapping("/kfc/brands")
public class JSONController {

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

        Shop shop = new Shop();
        shop.setName(name);
        shop.setStaffName(new String[]{"example1", "example2"});

        return shop;

    }

}

4. mvc: piloté par annotation

Activezmvc:annotation-driven dans votre fichier XML de configuration Spring.

mvc-dispatcher-servlet.xml



    

    

Télécharger le code source

Téléchargez-le -SpringMVC-Json-Example.zip (21 Ko)