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.</modelVersion> <groupId>csic</groupId>
      <artifactId>oa</artifactId>
      <version>0.0.-SNAPSHOT</version>
      <packaging>war</packaging> <name>oa</name>
      <url>http://maven.apache.org</url> <properties>
      <project.build.sourceEncoding>UTF-</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..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();
      return p;
      }
      @ResponseBody
      @RequestMapping("personxml")
      public Personxml personxml(){
      Personxml p=new Personxml();
      p.setName("jt");
      p.setAge();
      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;
      }
      }
  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..RELEASE</spring.version>
      </properties> <dependencies> <!-- Spring 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, ); 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.</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..RELEASE</spring.version>
        <jackson.version>1.9.</jackson.version>
        <jdk.version>1.6</jdk.version>
        </properties> <dependencies> <!-- Spring 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.</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>

Spring 梳理 - View - JSON and XML View的更多相关文章

  1. 工作的时候用到spring返回xml view查到此文章亲测可用

    spring mvc就是好,特别是rest风格的话,一个 org.springframework.web.servlet.view.ContentNegotiatingViewResolver就可以根 ...

  2. 封装fastjson为spring mvc的json view

    可以将其中的main方法删掉.测试用的.我测试的结果是,jackson比fastjson快. fastjson是1.1.36 jackson是2.2.3 jdk是1.7.40,client cpu是i ...

  3. 如何在Spring MVC Test中避免”Circular view path” 异常

    1. 问题的现象 比如在webConfig中定义了一个viewResolver public class WebConfig extends WebMvcConfigurerAdapter { //配 ...

  4. Android项目部署时,发生AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class错误

    这个错误也是让我纠结了一天,当时写的项目在安卓虚拟机上运行都很正常,于是当我部署到安卓手机上时,点击登陆按钮跳转到用户主界面的时候直接结束运行返回登陆界面.    当时,我仔细检查了一下自己的代码,并 ...

  5. bug_ _图片_android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>

    =========== 1   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zgan.communit ...

  6. bug_ _ android.view.InflateException: Binary XML file line #2: Error inflating class <unknown

    ========= 5.0     android异常“android.view.InflateException: Binary XML file line # : Error inflating ...

  7. Android(java)学习笔记200:Android中View动画之 XML实现 和 代码实现

    1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...

  8. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.L ...

  9. Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TabLayout,TableLayout引起页面崩溃

    在使用TableLayout的时候,运行引用程序直接Crash. FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 9703 java. ...

随机推荐

  1. Nacos高可用集群解决方案-Docker版本

    文章主旨 本文目的是配置高可用的Nacos集群 架构图 整体架构为:Nginx + 3 x Nacos +高可用MySQL 高可用MySQL使用主从复制结构的可以参考Docker搭建MySQL主从集群 ...

  2. 【StyleCop】StyleCop规则汇总

    所有规则的翻译(基于版本4.7.44.0): 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialElementsMustB ...

  3. Python中yield解析

    小探yield 查看 python yield 文档 yield expressions: Using a yield expression in a function's body causes t ...

  4. JIra配置权限方案

    目录: 添加用户 添加用户组 将用户分配到不同的组中 创建项目权限方案 配置项目采用的权限方案 1. 添加用户 1)使用admin权限的账户登录后,点击右上角的配置,选择system 2)在打开的页面 ...

  5. 2018 Petrozavodsk Winter Camp, Yandex Cup

    A. Ability Draft solved by RDC 60min start, 148 min AC, 1Y 题意:两只 Dota 队伍,每队 \(n\) 个英雄,英雄一开始无技能,他们需要按 ...

  6. 【欧拉降幂】Super_log

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  7. 移动端rem距离单位的使用

    在做移动端开发的时候大家肯定会遇到适配问题,手机的屏幕大小有非常多的类别,使用传统的px距离单位已经无法满足我们的需要,于是rem便横空出世,他与百分比定位是比较像的,但是也是有一定的区别,在这里就跟 ...

  8. C++ STL vector的学习

    vector就是一个不定长数组,vector是动态数组,随着元素的加入,它的内部机制会自行扩充空间以容纳新元素,使用vector之前,必须包含相应的头文件和命名空间. #include <vec ...

  9. 【Offer】[52] 【两个链表的第一个公共结点】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个链表,找出它们的第一个公共结点.下图中6为公共结点:  牛客网刷题地址 思路分析 如果两个链表有公共节点,那么公共节点出现在两 ...

  10. 【Offer】[32] 【从上到下打印二叉树】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 不换行:从上到下打印出二叉树的每个节点,同层的节点按照从左到右的顺序打印.例如,输入下图的二叉树,则依次打印出8,6,10,5,7,9, ...