Spring 3 MVC- und JSON-Beispiel

Beispiel für Spring 3 MVC und JSON

In diesem Tutorial zeigen wir Ihnen, wie Sie JSON-Daten im Spring MVC-Framework ausgeben.

Verwendete Technologien:

  1. Feder 3.2.2.FREIGABE

  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. Projektabhängigkeiten

Holen Sie sich Jackson und Spring Abhängigkeiten.

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. Modell

Ein einfaches POJO, das dieses Objekt später als formatierte JSON-Daten ausgibt.

package com.example.common.model;

public class Shop {

    String name;
    String staffName[];

    //getter and setter methods

}

3. Regler

Addiere@ResponseBody als Rückgabewert. Wen Spring sieht

  1. Die Jackson-Bibliothek ist im Projektklassenpfad vorhanden

  2. mvc:annotation-driven ist aktiviert

  3. Rückgabemethode mit @ResponseBody-Annotation

Spring übernimmt die JSON-Konvertierung automatisch.

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: annotationsgesteuert

Aktivieren Siemvc:annotation-driven in Ihrer Spring-Konfigurations-XML-Datei.

mvc-dispatcher-servlet.xml



    

    

Quellcode herunterladen

Laden Sie es herunter -SpringMVC-Json-Example.zip (21 KB)