实现标题这个议题,首先我们需要加入2个包,
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.5.6</version>
</dependency>
其次保证spring支持annotation,这里就不写配置了。
看controller:
@RequestMapping(value="/content-type/{prodId}")
@ResponseBody
public Map<String,List<ContentType>> listContentTypeByProdJSONFormat(HttpServletRequest request,@PathVariable("prodId") int prodId)
{
List<ContentType> contentTypeList = contentTypeService.listContentTypeByProduct(prodId);
Map<String,List<ContentType>> contentTypes = new HashMap<String,List<ContentType>>();
contentTypes.put("contentTypes", contentTypeList);
return contentTypes;
}
@ResponseBody注解就是要求这个controller以text文本形式返回,这里绑定一个Map类型,实际返回的就是个json对象,格式就是map转json的格式。这样在ajax调用的时候回调函数可以这样拿:
success:function(data)
{
var typeList = data.contentTypes;
var length = typeList.length;
....
....
....
});
这的contentTypes必须和后台controller绑定的Map的key保证一致,这个不用解释了吧。
分享到:
相关推荐
json的简单应用 使用json封装数据 html源代码 一个增加删除数据的例子
Spring返回json数据格式
spring使用jackson实现json
Spring MVC返回JSON数据 用到的jar包,主要是jackson-all-1.9.0.jar,jackson-all-1.9.9.jar,jackson-all-1.9.11.jar三个包
主要介绍了spring boot如何配置请求的入参和出参json数据格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring支持自动转json的依赖,我的博客文章《配置Spring支持自动json格式数据转换》中有详细说明,请自行搜索。(如果提供链接,资源被删~)
Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
Ajax With Spring 3. Eclipse 工程,包含Web所需要的 所有jar包。 1> ajax 请求。 2> spring 3注解使用 3> mvc:annotation
springMVC3.x 前后端使用json格式传输数据
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
最近公司项目出现这样一个...在spring框架下,可以利用ResponseBodyAdvice接口,实现该接口则能够在声明了ResponseBody注解的controller返回json数据前,统一对json数据做处理,将资源地址的url统一替换为cdn资源url。
使用Spring4 MVC 构建RESTful服务器,输出JSON格式的数据结构
NULL 博文链接:https://coocaobo.iteye.com/blog/2410629
org.json.jar spring-aop-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar spring-expression-4.1.6.RELEASE.jar spring-jdbc-4.1.6.RELEASE....
NULL 博文链接:https://gaojiewyh.iteye.com/blog/1754619
spring json ,返回数据,需要两个jar包
这是结合博客的源码,链接是:http://blog.csdn.net/u012660464/article/details/53463708 。名为:使用Spring MVC生成Json数据,我们后台开发生成了Json数据后可以供前端进行异步调用。
本资源是SpringMVC返回JSON数据的相关Jar包,结合我的博客教程可以完成与EasyUI DataGrid之间的数据交互。
假定两个Controller都在同一个工程中。 如果有比较合理的分层设计,这样的需求应该是非常...因为Controller B的实例中,Spring Boot为其进行配置和对象注入,这些准备工作不是简单通过实例化Controller B可以完成。
Spring+SpringMVC+MyBatis返回json所需jar包,SSM返回json数据,jackson包