1. <legend id='NGN3v'><style id='NGN3v'><dir id='NGN3v'><q id='NGN3v'></q></dir></style></legend>
      • <bdo id='NGN3v'></bdo><ul id='NGN3v'></ul>

      <i id='NGN3v'><tr id='NGN3v'><dt id='NGN3v'><q id='NGN3v'><span id='NGN3v'><b id='NGN3v'><form id='NGN3v'><ins id='NGN3v'></ins><ul id='NGN3v'></ul><sub id='NGN3v'></sub></form><legend id='NGN3v'></legend><bdo id='NGN3v'><pre id='NGN3v'><center id='NGN3v'></center></pre></bdo></b><th id='NGN3v'></th></span></q></dt></tr></i><div id='NGN3v'><tfoot id='NGN3v'></tfoot><dl id='NGN3v'><fieldset id='NGN3v'></fieldset></dl></div>
      <tfoot id='NGN3v'></tfoot>

    2. <small id='NGN3v'></small><noframes id='NGN3v'>

    3. Spring中的@RequestBody和@ResponseBody注解

      时间:2023-07-26

          <tbody id='pS1CM'></tbody>
        <i id='pS1CM'><tr id='pS1CM'><dt id='pS1CM'><q id='pS1CM'><span id='pS1CM'><b id='pS1CM'><form id='pS1CM'><ins id='pS1CM'></ins><ul id='pS1CM'></ul><sub id='pS1CM'></sub></form><legend id='pS1CM'></legend><bdo id='pS1CM'><pre id='pS1CM'><center id='pS1CM'></center></pre></bdo></b><th id='pS1CM'></th></span></q></dt></tr></i><div id='pS1CM'><tfoot id='pS1CM'></tfoot><dl id='pS1CM'><fieldset id='pS1CM'></fieldset></dl></div>

      1. <legend id='pS1CM'><style id='pS1CM'><dir id='pS1CM'><q id='pS1CM'></q></dir></style></legend>
      2. <small id='pS1CM'></small><noframes id='pS1CM'>

          1. <tfoot id='pS1CM'></tfoot>
              <bdo id='pS1CM'></bdo><ul id='pS1CM'></ul>
              • 本文介绍了Spring中的@RequestBody和@ResponseBody注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                有人能解释一下 Spring 3 中的 @RequestBody@ResponseBody 注释吗?它们是干什么用的?任何例子都会很棒.

                Can someone explain the @RequestBody and @ResponseBody annotations in Spring 3? What are they for? Any examples would be great.

                推荐答案

                文档中有一个完整的Section,叫做16.3.3.4 用@RequestBody注解映射请求体.还有一个叫做 16.3.3.5 使用@ResponseBody 注解映射响应体.我建议你查阅这些部分.也相关:@RequestBody javadocs, @ResponseBody javadocs

                There is a whole Section in the docs called 16.3.3.4 Mapping the request body with the @RequestBody annotation. And one called 16.3.3.5 Mapping the response body with the @ResponseBody annotation. I suggest you consult those sections. Also relevant: @RequestBody javadocs, @ResponseBody javadocs

                用法示例如下:

                使用 JQuery 之类的 JavaScript 库,您可以像这样发布 JSON 对象:

                Using a JavaScript-library like JQuery, you would post a JSON-Object like this:

                { "firstName" : "Elmer", "lastName" : "Fudd" }
                

                您的控制器方法如下所示:

                Your controller method would look like this:

                // controller
                @ResponseBody @RequestMapping("/description")
                public Description getDescription(@RequestBody UserStats stats){
                    return new Description(stats.getFirstName() + " " + stats.getLastname() + " hates wacky wabbits");
                }
                
                // domain / value objects
                public class UserStats{
                    private String firstName;
                    private String lastName;
                    // + getters, setters
                }
                public class Description{
                    private String description;
                    // + getters, setters, constructor
                }
                

                现在,如果您的类路径中有 Jackson(并且有一个 <mvc:annotation-driven> setup),Spring 会将传入的 JSON 转换为来自 post body 的 UserStats 对象(因为您添加了 @RequestBody 注释)并将返回的对象序列化为 JSON(因为您添加了 @ResponseBody 注释).所以浏览器/客户端会看到这个 JSON 结果:

                Now if you have Jackson on your classpath (and have an <mvc:annotation-driven> setup), Spring would convert the incoming JSON to a UserStats object from the post body (because you added the @RequestBody annotation) and it would serialize the returned object to JSON (because you added the @ResponseBody annotation). So the Browser / Client would see this JSON result:

                { "description" : "Elmer Fudd hates wacky wabbits" }
                

                有关完整的工作示例,请参阅我以前的答案:https://stackoverflow.com/a/5908632/342852

                See this previous answer of mine for a complete working example: https://stackoverflow.com/a/5908632/342852

                注意:RequestBody/ResponseBody 当然不限于 JSON,两者都可以处理多种格式,包括纯文本和 XML,但 JSON 可能是最常用的格式.

                Note: RequestBody / ResponseBody is of course not limited to JSON, both can handle multiple formats, including plain text and XML, but JSON is probably the most used format.

                从 Spring 4.x 开始,您通常不会在方法级别使用 @ResponseBody,而是在类级别使用 @RestController,效果相同.

                Ever since Spring 4.x, you usually won't use @ResponseBody on method level, but rather @RestController on class level, with the same effect.

                这里引用官方Spring MVC 文档:

                @RestController 是一个 组合注释 本身就是 元注释@Controller@ResponseBody 表示一个控制器每个方法都继承了类型级别的 @ResponseBody 注释,并且,因此,直接写入响应正文而不是视图解析并使用 HTML 模板进行渲染.

                @RestController is a composed annotation that is itself meta-annotated with @Controller and @ResponseBody to indicate a controller whose every method inherits the type-level @ResponseBody annotation and, therefore, writes directly to the response body versus view resolution and rendering with an HTML template.

                这篇关于Spring中的@RequestBody和@ResponseBody注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:我需要&lt;class&gt;persistence.xml 中的元素? 下一篇:XML 配置与基于注释的配置

                相关文章

                最新文章

              • <small id='iB7vC'></small><noframes id='iB7vC'>

                <i id='iB7vC'><tr id='iB7vC'><dt id='iB7vC'><q id='iB7vC'><span id='iB7vC'><b id='iB7vC'><form id='iB7vC'><ins id='iB7vC'></ins><ul id='iB7vC'></ul><sub id='iB7vC'></sub></form><legend id='iB7vC'></legend><bdo id='iB7vC'><pre id='iB7vC'><center id='iB7vC'></center></pre></bdo></b><th id='iB7vC'></th></span></q></dt></tr></i><div id='iB7vC'><tfoot id='iB7vC'></tfoot><dl id='iB7vC'><fieldset id='iB7vC'></fieldset></dl></div>

                1. <tfoot id='iB7vC'></tfoot>

                  1. <legend id='iB7vC'><style id='iB7vC'><dir id='iB7vC'><q id='iB7vC'></q></dir></style></legend>
                      <bdo id='iB7vC'></bdo><ul id='iB7vC'></ul>