Spring 梳理 - View - JSON and XML View

  1. 实际案例json
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>csic</groupId>
          <artifactId>oa</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>war</packaging>
      
          <name>oa</name>
          <url>http://maven.apache.org</url>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          </properties>
      
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <!-- Import dependency management from Spring Boot -->
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-dependencies</artifactId>
                      <version>2.1.1.RELEASE</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      
          <dependencies>
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>test</scope>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-devtools</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-freemarker</artifactId>
              </dependency>
          </dependencies>
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
      
              </plugins>
          </build>
      
      </project>

       

    2. package csic.oa.controller;
      
      import java.util.HashMap;
      
      import org.springframework.beans.factory.annotation.Autowired;
      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.ResponseBody;
      import org.springframework.web.bind.annotation.RestController;
      
      import csic.oa.domain.Person;
      import csic.oa.domain.Personxml;
      import csic.oa.service.UserService;
      
      //@RestController
      @Controller
      public class UserController {
      
          @ResponseBody
          @RequestMapping("person")
          public Person person(){
              Person p=new Person();
              p.setName("jt");
              p.setAge(20);
              return p;
          }
          @ResponseBody
          @RequestMapping("personxml")
          public Personxml personxml(){
              Personxml p=new Personxml();
              p.setName("jt");
              p.setAge(20);
              return p;
          }
          
      }

       

    3. package csic.oa.domain;
      
      public class Person {
          private String name;
          private int age;
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public int getAge() {
              return age;
          }
          public void setAge(int age) {
              this.age = age;
          }
      }

       

    4. package csic.oa.domain;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.*;
      
      @XmlRootElement(name="person")
      public class Personxml {
          private String name;
          private int age;
          public String getName() {
              return name;
          }
          @XmlElement
          public void setName(String name) {
              this.name = name;
          }
          public int getAge() {
              return age;
          }
          @XmlElement
          public void setAge(int age) {
              this.age = age;
          }
      }

       

    5.  

    6.  

  2. 实际案例xml
  3. To output JSON and XML views, you don’t need to do any extra works, Spring MVC will handle the conversion automatically. Read this Spring MVC and XML, and Spring MVC and JSON examples.
  4. XML example

    1.     <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. A simple POJO model and annotated with JAXB annotation, later convert this object into XML output.
      1. 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. Controller

      1. 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:annotation-driven

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

         

    5. Alternatively
      1. Alternatively, you can declares “spring-oxm.jar” dependency and include following MarshallingView, to handle the conversion. With this method, you don’t need annotate @ResponseBody in your method.
        
        <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. JSON example

    1. pom.xml
      1. <project xmlns="http://maven.apache.org/POM/4.0.0" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                http://maven.apache.org/maven-v4_0_0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.mkyong.common</groupId>
            <artifactId>SpringMVC</artifactId>
            <packaging>war</packaging>
            <version>1.0-SNAPSHOT</version>
            <name>SpringMVC Json Webapp</name>
            <url>http://maven.apache.org</url>
        
            <properties>
                <spring.version>3.2.2.RELEASE</spring.version>
                <jackson.version>1.9.10</jackson.version>
                <jdk.version>1.6</jdk.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>
        
                <!-- Jackson JSON Mapper -->
                <dependency>
                    <groupId>org.codehaus.jackson</groupId>
                    <artifactId>jackson-mapper-asl</artifactId>
                    <version>${jackson.version}</version>
                </dependency>
        
            </dependencies>
        
            <build>
                <finalName>SpringMVC</finalName>
                <plugins>
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>false</downloadJavadocs>
                        <wtpversion>2.0</wtpversion>
                    </configuration>
                  </plugin>
                  <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                    </configuration>
                  </plugin>
                </plugins>
            </build>
        
        </project>

         

    2. Model

      1. A simple POJO, later output this object as formatted JSON data.
      2. package com.mkyong.common.model;
        
        public class Shop {
        
            String name;
            String staffName[];
        
            //getter and setter methods
            
        }

         

    3. Controller

      1. Add @ResponseBody as return value. Wen Spring sees
        
            Jackson library is existed in the project classpath
            The mvc:annotation-driven is enabled
            Return method annotated with @ResponseBody
        
        Spring will handle the JSON conversion automatically.
        
        
        
        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.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[]{"mkyong1", "mkyong2"});
                
                return shop;
        
            }
            
        }

         

    4. mvc:annotation-driven

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

         

posted on 2019-01-01 17:05  手握太阳  阅读(173)  评论(0编辑  收藏  举报

导航