网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Springmvc4.x利用@ResponseBody返回Json数据的方法

下面是官方文档对于@ResponseBody注解的解释:

成都创新互联主营江干网站建设的网络公司,主营网站建设方案,成都APP应用开发,江干h5小程序定制开发搭建,江干网站营销推广欢迎江干等地区企业咨询

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:

  
      
        
          
        
      
    
    
      
        
        text/html;charset=UTF-8  
        text/json;charset=UTF-8  
        application/json;charset=UTF-8  
        
      
   

下面贴出在官方文档中的位置:

Springmvc 4.x利用@ResponseBody返回Json数据的方法

这个需要jackson jar包支持,需要 jackson-annotations,jackson-core,jackson-databind三个包,:

Springmvc 4.x利用@ResponseBody返回Json数据的方法

控制器代码:

@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; 
  }

jquery代码:

//|增加 
    $("#hanblog_add_btn").click(function(){ 
      var classname=$("#add_input_name").val(); 
      var classintroduction=$("#add_input_introduction").val(); 
      alert("分类名称:"+classname+"分类介绍:"+classintroduction); 
      $.get("",function(result){ 
        alert(result); 
      }); 
    }); 

运行返回例子:

Springmvc 4.x利用@ResponseBody返回Json数据的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


名称栏目:Springmvc4.x利用@ResponseBody返回Json数据的方法
文章转载:http://bjjierui.cn/article/pjdoii.html

其他资讯