java restful返回json_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的依赖:

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">

4.0.0

com.mkyong.common

SpringMVC

war

1.0-SNAPSHOT

SpringMVC Json Webapp

http://maven.apache.org

3.2.2.RELEASE

1.9.10

1.6

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.codehaus.jackson

jackson-mapper-asl

${jackson.version}

SpringMVC

org.apache.maven.plugins

maven-eclipse-plugin

2.9

true

false

2.0

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

${jdk.version}

${jdk.version}

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注解。

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">

5、示例结果

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

spring-mvc-json-demo

a52f92bd9b2acfb18acd41330f5736a3.png

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值