Spring @ResponseBody注释如何在这个RESTful应用程序示例中工作? [英] How does the Spring @ResponseBody annotation work in this RESTful application example?

查看:160
本文介绍了Spring @ResponseBody注释如何在这个RESTful应用程序示例中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按以下方式注释的方法:

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 对网址 / 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?

它位于返回的<$之前c $ c>列出< 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 $ b写入HTTP响应$ b消息转换器(而不是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

似乎需要列出<帐户> 对象并将其放入 Http响应。这是正确的还是我误解?

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



<那么究竟是什么意思呢?这是否意味着 accountSummary()方法返回的 List< Account> 对象会自动转换为 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.

格式的选择取决于已安装的消息转换器,取决于 生成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注释如何在这个RESTful应用程序示例中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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