Spring @ResponseBody批注如何工作? [英] How does the Spring @ResponseBody annotation work?

查看:114
本文介绍了Spring @ResponseBody批注如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种通过以下方式注释的方法:

I have a method that is annotated in the following way:

/**
* Provide a list of all accounts.
*/
//  TODO 02: Complete this method.  Add annotations to respond
//  to GET /accounts and return a List<Account> to be converted.
//  Save your work and restart the server.  You should get JSON results when accessing 
//  http://localhost:8080/rest-ws/app/accounts
@RequestMapping(value="/orders", method=RequestMethod.GET)
public @ResponseBody List<Account> accountSummary() {
    return accountManager.getAllAccounts();
}

所以我知道这个注释:

@RequestMapping(value="/orders", method=RequestMethod.GET)

此方法处理 GET 对由URL /orders 表示的资源发出的HTTP请求.

this method handle GET HTTP requests made to the resource represented by the URL /orders.

此方法调用DAO对象,该对象返回列表.

This method calls a DAO object that returns a List.

其中帐户代表系统上的用户,并具有一些代表该用户的字段,例如:

where Account represents a user on the system and has some fields that represent this user, something like:

public class Account {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long entityId;

    @Column(name = "NUMBER")
    private String number;

    @Column(name = "NAME")
    private String name;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name = "ACCOUNT_ID")
    private Set<Beneficiary> beneficiaries = new HashSet<Beneficiary>();

    ...............................
    ...............................
    ...............................
}

我的问题是: @ResponseBody注释的工作原理是什么?

My question is: How exactly the does the @ResponseBody annotation work?

它位于返回的List<Account>对象之前,因此我认为它引用了此List.该课程文档指出,该注释可用于以下功能:

It is situated before the returned List<Account> object so I think that it refers to this List. The course documentation states that this annotation serves the function to:

确保将结果通过HTTP写入HTTP响应 邮件转换器(而不是MVC视图).

ensure that the result will be written to the HTTP response by an HTTP Message Converter (instead of an MVC View).

还要阅读Spring的官方文档:

And also reading on the official Spring documentation: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html

似乎它采用了List<Account>对象并将其放入Http Response中.这是正确的还是我误会了?

it seems that it takes the List<Account> object and puts it into the Http Response. Is this correct or am I misunderstanding?

写在以前的accountSummary()方法的注释中:

Written into the comment of the previous accountSummary() method there is:

访问时应获取JSON结果 http://localhost:8080/rest-ws/app/accounts

You should get JSON results when accessing http://localhost:8080/rest-ws/app/accounts

那么这到底是什么意思?这是否意味着accountSummary()方法返回的List<Account>对象会自动转换为JSON格式,然后放入Http Response中?还是什么?

So what exactly does this mean? Does it mean that the List<Account> object returned by the accountSummary() method is automatically converted into JSON format and then put into the Http Response? Or what?

如果此声明为true,则在何处指定对象将自动转换为JSON格式?使用@ResponseBody批注时是采用标准格式还是在其他地方指定?

If this assertion is true, where is it specified that the object will be automatically converted into JSON format? Is the standard format adopted when the @ResponseBody annotation is used or is it specified elsewhere?

推荐答案

首先,注释不会注释List.就像RequestMapping一样,它对方法进行了注释.您的代码等同于

First of all, the annotation doesn't annotate List. It annotates the method, just as RequestMapping does. Your code is equivalent to

@RequestMapping(value="/orders", method=RequestMethod.GET)
@ResponseBody
public List<Account> accountSummary() {
    return accountManager.getAllAccounts();
}

现在注释的意思是方法的返回值将构成HTTP响应的主体.当然,HTTP响应不能包含Java对象.因此,此帐户列表将转换为适合REST应用程序的格式,通常为JSON或XML.

Now what the annotation means is that the returned value of the method will constitute the body of the HTTP response. Of course, an HTTP response can't contain Java objects. So this list of accounts is transformed to a format suitable for REST applications, typically JSON or XML.

格式的选择取决于已安装的消息转换器,取决于

The choice of the format depends on the installed message converters, on the values of the produces attribute of the RequestMapping annotation, and on the content type that the client accepts (that is available in the HTTP request headers). For example, if the request says it accepts XML, but not JSON, and there is a message converter installed that can transform the list to XML, then XML will be returned.

这篇关于Spring @ResponseBody批注如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆