Springmvc 4.x利用@ResponseBody返回Json数据
来源:互联网 发布:网络销售技巧和话术 编辑:程序博客网 时间:2024/05/17 06:11
点击打开链接
@ResopnseBody注解能够 直接把 控制器返回变量(String)直接 返回给浏览器,也可以通过配置 后,把 对象 序列化成Json数据返回给浏览器!如果为 null 就会返回空白。下面贴出在官方文档中的位置:jquery代码:运行返回例子:
下面是官方文档对于@ResponseBody注解的解释:
- Mapping the response body with the @ResponseBody annotation
- The @ResponseBody annotation is similar to @RequestBody. This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name). For example:
- @RequestMapping(path = "/something", method = RequestMethod.PUT)
- @ResponseBody
- public String helloWorld() {
- return "Hello World";
- }
- The above example will result in the text Hello World being written to the HTTP response stream.
- As with @RequestBody, Spring converts the returned object to a response body by using an HttpMessageConverter. For more information on these converters, see the previous section and Message Converters.
@ResopnseBody注解能够 直接把 控制器返回变量(String)直接 返回给浏览器,也可以通过配置 后,把 对象 序列化成Json数据返回给浏览器!如果为 null 就会返回空白。
怎么配置呢 ?需要配置MessageConverter:
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <ref bean="mappingJackson2HttpMessageConverter" />
- </list>
- </property>
- </bean>
- <bean id="mappingJackson2HttpMessageConverter"
- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- <value>text/json;charset=UTF-8</value>
- <value>application/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
这个需要jackson jar包支持,需要 jackson-annotations,jackson-core,jackson-databind三个包,:
控制器代码:
- @RequestMapping("House/ClassManager/addByAjax")
- @ResponseBody
- public HanBlog_Class ClassManager_addByAjax(HttpServletRequest request){
- if(request.getSession().getAttribute("hanblog_uid")==null) return null;
- HanBlog_Class objClass=new HanBlog_Class();
- return objClass;
- }
- //|增加
- $("#hanblog_add_btn").click(function(){
- var classname=$("#add_input_name").val();
- var classintroduction=$("#add_input_introduction").val();
- alert("分类名称:"+classname+"分类介绍:"+classintroduction);
- $.get("<c:url value="/House/ClassManager/addByAjax.do" />",function(result){
- alert(result);
- });
- });
0 0
- Springmvc 4.x利用@ResponseBody返回Json数据
- Springmvc 4.x利用@ResponseBody返回Json数据
- Springmvc 4.x利用@ResponseBody返回Json数据
- springmvc @ResponseBody 返回json数据错误
- springmvc @ResponseBody 返回json数据错误
- springmvc配置responsebody返回json格式数据
- JSON解析类库之Gson(7) --- Gson与SpringMVC的整合,利用@ResponseBody返回Json数据
- springMVC使用@ResponseBody返回json
- SpringMVC@ResponseBody返回JSON乱码
- easyui ajax请求获取SpringMVC @ResponseBody返回Json数据
- SpringMVC返回json数据(@ResponseBody注解的使用)
- SpringMVC @ResponseBody返回json数据 浏览器406 error 解析
- Spring 4.x 中使用注解 @ResponseBody 返回json数据的配置
- springmvc使用@ResponseBody返回json乱码解决方法
- springmvc无法通过@ResponseBody返回json问题解决
- SpringMVC @ResponseBody 返回json date 转换
- SpringMVC ResponseBody返回字符串(JSON)乱码
- SpringMVC @ResponseBody 返回json date 转换
- web应用架构扩展
- error C2440: “static_cast”: 无法从“void (__thiscall CXPButton::* )(WPARAM,LPARAM)”转换为“LRESULT (__thisca
- C/C++无限关机(提权例子)
- 如何用IDEA一步一步开发WebService服务器端
- j2ee的web工程,研发过程中,无论什么架构和框架(单工程的、前后端分离的、Struts、spring等),都离不开一个问题,前后端如何传参。那么,传参究竟是什么意思呢?
- Springmvc 4.x利用@ResponseBody返回Json数据
- 《Java源码解析》之NIO的Selector机制(Part3:Selector.select())
- ViewBag&&ViewData
- 高CPU调试(一)
- html 的input标签 各种输入控制
- 如何用IDEA一步一步开发WebService客户端程序
- 敌军布阵(线段数)
- 第十四周项目4矩阵运算(1)矩阵相加
- JMeter学习-010-JMeter 配置元件实例之 - CSV Data Set Config 参数化配置