发布于 2017-09-07 04:48:04 | 285 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


这篇文章主要介绍了Spring mvc实现Restful返回json格式数据实例详解的相关资料,需要的朋友可以参考下

在本示例中,我们将向您展示如何将对象转换成json格式并通过spring mvc框架返回给用户。

使用技术及环境:

  • Spring 3.2.2.RELEASE
  • Jackson 1.9.10
  • JDK 1.6
  • Eclipse 3.6
  • Maven 3

PS:在spring 3 中,要输出json数据,只需要添加Jackson 库到你的classpath。

1、项目依赖

spring和jackson的依赖:


<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

一个简单的JavaBean,稍后将被转换成json格式输出。


public class Shop {
 String name;
 String staffName[];
 //getter and setter methods
}

3、Controller

添加@ResponseBody到返回值,我们看到:

Jackson 包已经在项目的 classpath

mvc:annotation-driven注解已经启用

返回方法已经添加了@ResponseBody

spring会自动处理json的转换。


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](https://my.oschina.net/u/1774615)
@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

在你的spring配置文件中启用mvc:annotation-driven注解。


<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、示例结果

访问URL:http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar

spring-mvc-json-demo

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务