1. 实际案例json
      1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      3. <modelVersion>4.0.</modelVersion>
      4.  
      5. <groupId>csic</groupId>
      6. <artifactId>oa</artifactId>
      7. <version>0.0.-SNAPSHOT</version>
      8. <packaging>war</packaging>
      9.  
      10. <name>oa</name>
      11. <url>http://maven.apache.org</url>
      12.  
      13. <properties>
      14. <project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
      15. </properties>
      16.  
      17. <dependencyManagement>
      18. <dependencies>
      19. <dependency>
      20. <!-- Import dependency management from Spring Boot -->
      21. <groupId>org.springframework.boot</groupId>
      22. <artifactId>spring-boot-dependencies</artifactId>
      23. <version>2.1..RELEASE</version>
      24. <type>pom</type>
      25. <scope>import</scope>
      26. </dependency>
      27. </dependencies>
      28. </dependencyManagement>
      29.  
      30. <dependencies>
      31. <dependency>
      32. <groupId>junit</groupId>
      33. <artifactId>junit</artifactId>
      34. <version>4.12</version>
      35. <scope>test</scope>
      36. </dependency>
      37. <dependency>
      38. <groupId>org.springframework.boot</groupId>
      39. <artifactId>spring-boot-starter-web</artifactId>
      40. </dependency>
      41. <dependency>
      42. <groupId>org.springframework.boot</groupId>
      43. <artifactId>spring-boot-devtools</artifactId>
      44. </dependency>
      45. <dependency>
      46. <groupId>org.springframework.boot</groupId>
      47. <artifactId>spring-boot-starter-freemarker</artifactId>
      48. </dependency>
      49. </dependencies>
      50. <build>
      51. <plugins>
      52. <plugin>
      53. <groupId>org.springframework.boot</groupId>
      54. <artifactId>spring-boot-maven-plugin</artifactId>
      55. </plugin>
      56.  
      57. </plugins>
      58. </build>
      59.  
      60. </project>
      1. package csic.oa.controller;
      2.  
      3. import java.util.HashMap;
      4.  
      5. import org.springframework.beans.factory.annotation.Autowired;
      6. import org.springframework.stereotype.Controller;
      7. import org.springframework.web.bind.annotation.PathVariable;
      8. import org.springframework.web.bind.annotation.RequestMapping;
      9. import org.springframework.web.bind.annotation.ResponseBody;
      10. import org.springframework.web.bind.annotation.RestController;
      11.  
      12. import csic.oa.domain.Person;
      13. import csic.oa.domain.Personxml;
      14. import csic.oa.service.UserService;
      15.  
      16. //@RestController
      17. @Controller
      18. public class UserController {
      19.  
      20. @ResponseBody
      21. @RequestMapping("person")
      22. public Person person(){
      23. Person p=new Person();
      24. p.setName("jt");
      25. p.setAge();
      26. return p;
      27. }
      28. @ResponseBody
      29. @RequestMapping("personxml")
      30. public Personxml personxml(){
      31. Personxml p=new Personxml();
      32. p.setName("jt");
      33. p.setAge();
      34. return p;
      35. }
      36.  
      37. }
      1. package csic.oa.domain;
      2.  
      3. public class Person {
      4. private String name;
      5. private int age;
      6. public String getName() {
      7. return name;
      8. }
      9. public void setName(String name) {
      10. this.name = name;
      11. }
      12. public int getAge() {
      13. return age;
      14. }
      15. public void setAge(int age) {
      16. this.age = age;
      17. }
      18. }
      1. package csic.oa.domain;
      2. import javax.xml.bind.annotation.XmlRootElement;
      3. import javax.xml.bind.annotation.*;
      4.  
      5. @XmlRootElement(name="person")
      6. public class Personxml {
      7. private String name;
      8. private int age;
      9. public String getName() {
      10. return name;
      11. }
      12. @XmlElement
      13. public void setName(String name) {
      14. this.name = name;
      15. }
      16. public int getAge() {
      17. return age;
      18. }
      19. @XmlElement
      20. public void setAge(int age) {
      21. this.age = age;
      22. }
      23. }
  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>
      2. <spring.version>3.0..RELEASE</spring.version>
      3. </properties>
      4.  
      5. <dependencies>
      6.  
      7. <!-- Spring dependencies -->
      8. <dependency>
      9. <groupId>org.springframework</groupId>
      10. <artifactId>spring-core</artifactId>
      11. <version>${spring.version}</version>
      12. </dependency>
      13.  
      14. <dependency>
      15. <groupId>org.springframework</groupId>
      16. <artifactId>spring-web</artifactId>
      17. <version>${spring.version}</version>
      18. </dependency>
      19.  
      20. <dependency>
      21. <groupId>org.springframework</groupId>
      22. <artifactId>spring-webmvc</artifactId>
      23. <version>${spring.version}</version>
      24. </dependency>
      25.  
      26. </dependencies>
    1. A simple POJO model and annotated with JAXB annotation, later convert this object into XML output.
        1. package com.mkyong.common.model;
        2.  
        3. import javax.xml.bind.annotation.XmlElement;
        4. import javax.xml.bind.annotation.XmlRootElement;
        5.  
        6. @XmlRootElement(name = "coffee")
        7. public class Coffee {
        8.  
        9. String name;
        10. int quanlity;
        11.  
        12. public String getName() {
        13. return name;
        14. }
        15.  
        16. @XmlElement
        17. public void setName(String name) {
        18. this.name = name;
        19. }
        20.  
        21. public int getQuanlity() {
        22. return quanlity;
        23. }
        24.  
        25. @XmlElement
        26. public void setQuanlity(int quanlity) {
        27. this.quanlity = quanlity;
        28. }
        29.  
        30. public Coffee(String name, int quanlity) {
        31. this.name = name;
        32. this.quanlity = quanlity;
        33. }
        34.  
        35. public Coffee() {
        36. }
        37.  
        38. }
    2. Controller

        1. package com.mkyong.common.controller;
        2.  
        3. import org.springframework.stereotype.Controller;
        4. import org.springframework.web.bind.annotation.PathVariable;
        5. import org.springframework.web.bind.annotation.RequestMapping;
        6. import org.springframework.web.bind.annotation.RequestMethod;
        7. import org.springframework.web.bind.annotation.ResponseBody;
        8. import com.mkyong.common.model.Coffee;
        9.  
        10. @Controller
        11. @RequestMapping("/coffee")
        12. public class XMLController {
        13.  
        14. @RequestMapping(value="{name}", method = RequestMethod.GET)
        15. public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {
        16.  
        17. Coffee coffee = new Coffee(name, );
        18.  
        19. return coffee;
        20.  
        21. }
        22.  
        23. }
    3. mvc:annotation-driven

        1. <beans xmlns="http://www.springframework.org/schema/beans"
        2. xmlns:context="http://www.springframework.org/schema/context"
        3. xmlns:mvc="http://www.springframework.org/schema/mvc"
        4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        5. xsi:schemaLocation="
        6. http://www.springframework.org/schema/beans
        7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        8. http://www.springframework.org/schema/context
        9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
        10. http://www.springframework.org/schema/mvc
        11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        12.  
        13. <context:component-scan base-package="com.mkyong.common.controller" />
        14.  
        15. <mvc:annotation-driven />
        16.  
        17. </beans>
    4. Alternatively
        1. Alternatively, you can declares spring-oxm.jar dependency and include following MarshallingView, to handle the conversion. With this method, you dont need annotate @ResponseBody in your method.
        2.  
        3. <beans ...>
        4. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
        5.  
        6. <bean id="xmlViewer"
        7. class="org.springframework.web.servlet.view.xml.MarshallingView">
        8. <constructor-arg>
        9. <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        10. <property name="classesToBeBound">
        11. <list>
        12. <value>com.mkyong.common.model.Coffee</value>
        13. </list>
        14. </property>
        15. </bean>
        16. </constructor-arg>
        17. </bean>
        18. </beans>
  5. JSON example

    1. pom.xml
        1. <project xmlns="http://maven.apache.org/POM/4.0.0"
        2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
        4. http://maven.apache.org/maven-v4_0_0.xsd">
        5. <modelVersion>4.0.</modelVersion>
        6. <groupId>com.mkyong.common</groupId>
        7. <artifactId>SpringMVC</artifactId>
        8. <packaging>war</packaging>
        9. <version>1.0-SNAPSHOT</version>
        10. <name>SpringMVC Json Webapp</name>
        11. <url>http://maven.apache.org</url>
        12.  
        13. <properties>
        14. <spring.version>3.2..RELEASE</spring.version>
        15. <jackson.version>1.9.</jackson.version>
        16. <jdk.version>1.6</jdk.version>
        17. </properties>
        18.  
        19. <dependencies>
        20.  
        21. <!-- Spring dependencies -->
        22. <dependency>
        23. <groupId>org.springframework</groupId>
        24. <artifactId>spring-core</artifactId>
        25. <version>${spring.version}</version>
        26. </dependency>
        27.  
        28. <dependency>
        29. <groupId>org.springframework</groupId>
        30. <artifactId>spring-web</artifactId>
        31. <version>${spring.version}</version>
        32. </dependency>
        33.  
        34. <dependency>
        35. <groupId>org.springframework</groupId>
        36. <artifactId>spring-webmvc</artifactId>
        37. <version>${spring.version}</version>
        38. </dependency>
        39.  
        40. <!-- Jackson JSON Mapper -->
        41. <dependency>
        42. <groupId>org.codehaus.jackson</groupId>
        43. <artifactId>jackson-mapper-asl</artifactId>
        44. <version>${jackson.version}</version>
        45. </dependency>
        46.  
        47. </dependencies>
        48.  
        49. <build>
        50. <finalName>SpringMVC</finalName>
        51. <plugins>
        52. <plugin>
        53. <groupId>org.apache.maven.plugins</groupId>
        54. <artifactId>maven-eclipse-plugin</artifactId>
        55. <version>2.9</version>
        56. <configuration>
        57. <downloadSources>true</downloadSources>
        58. <downloadJavadocs>false</downloadJavadocs>
        59. <wtpversion>2.0</wtpversion>
        60. </configuration>
        61. </plugin>
        62. <plugin>
        63. <groupId>org.apache.maven.plugins</groupId>
        64. <artifactId>maven-compiler-plugin</artifactId>
        65. <version>2.3.</version>
        66. <configuration>
        67. <source>${jdk.version}</source>
        68. <target>${jdk.version}</target>
        69. </configuration>
        70. </plugin>
        71. </plugins>
        72. </build>
        73.  
        74. </project>
    2. Model

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

        1. Add @ResponseBody as return value. Wen Spring sees
        2.  
        3. Jackson library is existed in the project classpath
        4. The mvc:annotation-driven is enabled
        5. Return method annotated with @ResponseBody
        6.  
        7. Spring will handle the JSON conversion automatically.
        8.  
        9. package com.mkyong.common.controller;
        10.  
        11. import org.springframework.stereotype.Controller;
        12. import org.springframework.web.bind.annotation.PathVariable;
        13. import org.springframework.web.bind.annotation.RequestMapping;
        14. import org.springframework.web.bind.annotation.RequestMethod;
        15. import org.springframework.web.bind.annotation.ResponseBody;
        16. import com.mkyong.common.model.Shop;
        17.  
        18. @Controller
        19. @RequestMapping("/kfc/brands")
        20. public class JSONController {
        21.  
        22. @RequestMapping(value="{name}", method = RequestMethod.GET)
        23. public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
        24.  
        25. Shop shop = new Shop();
        26. shop.setName(name);
        27. shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
        28.  
        29. return shop;
        30.  
        31. }
        32.  
        33. }
    4. mvc:annotation-driven

        1. <beans xmlns="http://www.springframework.org/schema/beans"
        2. xmlns:context="http://www.springframework.org/schema/context"
        3. xmlns:mvc="http://www.springframework.org/schema/mvc"
        4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        5. xsi:schemaLocation="
        6. http://www.springframework.org/schema/beans
        7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        8. http://www.springframework.org/schema/context
        9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
        10. http://www.springframework.org/schema/mvc
        11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        12.  
        13. <context:component-scan base-package="com.mkyong.common.controller" />
        14.  
        15. <mvc:annotation-driven />
        16.  
        17. </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. stage_ros的world文件配置方法

    官方文档参阅:http://rtv.github.io/Stage/modules.html stage_ros是一个基于stage的2D模拟器,用于ROS的仿真测试.虽然现在越来越多的人在使用gaz ...

  2. python控制窗口口字形运动

    import win32con import win32gui import time import math notepad = win32gui.FindWindow("Photo_Li ...

  3. CF-920C-Swap Adjacent Elements 贪心

    题意 给你一个1-n的排列. 并给你一个字符串——其中用0和1表示对应数列中的位置上的值可不可以和后面相邻的数交换. 判断该数列能否在限制中交换为不降序数列. 思路 由于刚学了树状数组,一开始以为是用 ...

  4. HDU3652:B-number(数位DP)

    Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for ...

  5. 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...

  6. 了解css中px、em、rem的区别并使用Flexible实现vue移动端的适配

    本人java菜鸟一名,若有错误,还请见谅. 1.px和em和rem的定义和区别 px:px像素,是相对单位,相对于屏幕的分辨率而言,也就是说,当屏幕的分辨率不同那么px相同,实际看到的大小也会不同. ...

  7. LVM的创建及管理

    创建及管理LVM分区. Lvm(logical  volume  manager)逻辑卷管理 作用:动态调整磁盘容量,提高磁盘管理的灵活性. 注意:/boot分区用于存放引导文件,不能基于LVM创建. ...

  8. docker容器内 java应用程序启动慢

    原谅我对JVM 不是很熟悉. 参考http://hongjiang.info/tomcat-startup-slowly-in-docker/ 感谢作者.

  9. Maven工程读取properties文件过程

    1.创建需要读取的properties文件 2.在xml文件中加载配置文件 <!-- 加载配置文件 --> <context:property-placeholder locatio ...

  10. eclipse中离线安装activit插件

    离线安装activiti教程: 1.先下载压缩包和jar包 链接:https://pan.baidu.com/s/1hSToZt_4A262rUxc8KToCw 密码:j5r1 2.将下载好的jars ...