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

查看:24
本文介绍了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();
}

所以我知道通过这个注释:

So I know that by this annotation:

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

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

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

这个方法调用一个返回List的DAO对象.

This method calls a DAO object that returns a List.

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

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 对象之前,所以我认为它指的是这个 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 文档:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html

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 对象并将其放入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对象会自动转换成JSON格式,然后放入<代码>Http响应?或者什么?

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?

如果这个断言为真,那么在哪里指定对象将自动转换为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.

格式的选择取决于安装的消息转换器,取决于 produces 属性 @RequestMapping 注释,以及内容类型客户端接受(在 HTTP 请求标头中可用).例如,如果请求说它接受 XML,但不接受 JSON,并且安装了可以将列表转换为 XML 的消息转换器,则将返回 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天全站免登陆