博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC之@ResponseBody
阅读量:7004 次
发布时间:2019-06-27

本文共 591 字,大约阅读时间需要 1 分钟。

hot3.png

@ResponseBody

作用:

    该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

    返回的数据不是html标签的页面,而是其他格式的数据时(eg:json、xml)使用;需要注意的是,在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。

example:

@RequestMapping("/login")

@ResponseBody

public User login(User user){

    return user;

}

User 字段:userName,pwd

那么在前台接收到的数据为:{“userName”:"xxx","pwd":"xxx"}

效果等同于如下代码:

@RequestMapping("/login")

public void login(User user,HttpServletResponse response){

    response.getWritter.write(JSONObject.fromObject(user).toString());

}

转载于:https://my.oschina.net/inchlifc/blog/1574309

你可能感兴趣的文章