Spring MVC生成JSON数据
以下示例演示如何使用Spring Web MVC框架生成JSON数据格式。首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
- 创建一个名称为 GenerateJson 的动态WEB项目。
- 在
com.yiibai.springmvc
包下创建三个Java类:User
和UserController
。 - 从Maven仓库页面下载Jackson库:Jackson Core,Jackson Databind和Jackson Annotations。把它们放在
CLASSPATH
中。 - 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。
完整的项目文件目录结构如下所示 -
User.java 的代码如下所示 -
package com.yiibai.springmvc;
public class User {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
UserController.java 的代码如下所示 -
package com.yiibai.springmvc;
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;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody User getUser(@PathVariable String name) {
User user = new User();
user.setName(name);
user.setId(1100);
return user;
}
}
GenerateJson-servlet.xml 配置如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.yiibai.springmvc" />
<mvc:annotation-driven />
</beans>
在上面的代码中创建了一个简单的POJO用户,在UserController
中返回了User
对象。 Spring基于类路径中的RequestMapping
和Jackson jar
自动处理JSON转换。
完成创建源和配置文件后,发布应用程序到Tomcat服务器。
现在启动Tomcat服务器,当访问URL => http://localhost:8080/GenerateJson/user/yiibai , 如果Spring Web应用程序没有问题,应该看到以下结果:
Spring MVC生成JSON数据的更多相关文章
- ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误
ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...
- Spring MVC之JSON数据交互和RESTful的支持
1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...
- Spring MVC返回json数据给Android端
原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...
- spring mvc 返回json数据的四种方式
一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...
- Spring MVC 返回json数据 报406错误 问题解决方案
将jackson jar包改为jackson-databind-2.5.0.jar jackson-core-2.5.0.jar jackson-annotations-2.5.0.jar(这个版 ...
- spring mvc controller json数据
项目中遇到个批处理,需要前台传递一个json格式对象数组,如下:var data={ "wos":[{"id":1,"satisfaction&q ...
- spring mvc 返回JSON数据
servlet相关XML配置 <!--这个不启用会报错--><mvc:annotation-driven /> <bean class="org.springf ...
- Spring MVC返回JSON数据
将一个对象以json数据格式返回前台: @ResponseBody public User login(User user) { return user; } 在控制器上使用@ResponseBod ...
- spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable
1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...
随机推荐
- nativeexcel数据集导出excel
nativeexcel数据集导出excel uses Dataset2Excel; procedure Tfdm.exportXLS(dataset: TDataSet);var xls: TData ...
- nginx+lua+redis
git clone --branch master https://github.com/openresty/lua-resty-redis.git yum install openssl opens ...
- PHP微信墙制作
微信墙 PHP 注意:由于微信官网不定时会更新,其中模拟登陆以及爬取数据的方式可能会失效,最近这12个月里,就有两次更新导致此功能需要重写. 服务端源码->github地址传送门 思路 其实实现 ...
- 【Git】Git hangs while unpacking objects (Windows)
Git hangs while unpacking objects (Windows) 14 Oct 2014 I'm not sure if this is because we're behind ...
- python对solr进行查询、插入操作(GET\POST)
循环查询一个列表中词的个数: #coding=utf-8 import requests ls = [u'我', u'是', u'一个', u'天才', u'你', u'也是'] for item i ...
- 转:JavaWeb学习总结(一) 写得相当不错
这个系列写的相当不错,浅显易懂,图文并茂 http://www.cnblogs.com/xdp-gacl/tag/JavaWeb%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93 ...
- Netty 中文教程 (二) Hello World !详解
1.HelloServer 详解 HelloServer首先定义了一个静态终态的变量---服务端绑定端口7878.至于为什么是这个7878端口,纯粹是笔者个人喜好.大家可以按照自己的习惯选择端口.当然 ...
- 在LoadRunner中设置HTTP请求time-out的时间
Error -27728: Step download timeout (120 seconds) has expired when downloading non-resource(s) [MsgI ...
- HTML ui ol dl
<!-- 超链接target的属性 _blank 在新窗体中打开被链接文档. _self 默认. 在同样的框架中打开被链接文档. _parent 在父框架集中打开被链接文档. _top 在整个窗 ...
- 应用phpexcel导出excel文件后打不开的问题解决方法
应用phpexcel导出excel文件后打不开,提示“文件格式或文件扩展名无效,请确定文件未损坏,并且文件扩展名与文件的格式匹配”. 试了以下方法: 1.首先区分文件格式是2003,还是2007. 参 ...