欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring mvc实现Restful返回json格式数据实例详解

程序员文章站 2023-08-13 22:18:59
在本示例中,我们将向您展示如何将对象转换成json格式并通过spring mvc框架返回给用户。 使用技术及环境: spring 3.2.2.release...

在本示例中,我们将向您展示如何将对象转换成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

Spring mvc实现Restful返回json格式数据实例详解

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