springMVC中的 responseBody注解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38323645/article/details/82832312

@responseBody注解

@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。

在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中:

/*
User对象:userName pwd
那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}',即,一个json串
*/
@RequestMapping("/aa")
@ResponseBody
public User aa(User user){
    return user;
}

他的效果等同于通过response对象输出指定格式的数据:

/*
User对象:userName pwd
那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}',即,一个json串
*/
@RequestMapping("/aa")
public void aa(User user, HttpServletResponse response){
   response.getWriter.write(JSONObject.fromObject(user).toString());
}

猜你喜欢

转载自blog.csdn.net/weixin_38323645/article/details/82832312