- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 180字
- 2021-07-02 19:38:21
Using @ResponseBody annotation
This section represents the concepts related to the usage of the @ResponseBody annotation for returning a response to the client request.
The @ResponseBody annotation can be applied both at the class level and the method level. When @ResponseBody is applied at the class level along with the @Controller annotation, another annotation such as @RestController can be used instead.
The @ResonseBody annotation represents the fact that the value returned by the method will form the body of the response. When the value returned is an object, the object is converted into an appropriate JSON or XML format by HttpMessageConverters. The format is decided based on the value of the produce attribute of the @RequestMapping annotation, and also the type of content that the client accepts. Take a look at the following example:
@Controller
public class RestDemoController {
@RequestMapping(value="/hello", method=RequestMethod.POST, produces="application/json")
@ResponseBody
public HelloMessage getHelloMessage(@RequestBody User user) {
HelloMessage helloMessage = new HelloMessage();
String name = user.getName();
helloMessage.setMessage( "Hello " + name + "! How are you doing?");
helloMessage.setName(name);
return helloMessage;
}
}
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- 深度實踐OpenStack:基于Python的OpenStack組件開發(fā)
- PWA入門與實踐
- Computer Vision for the Web
- Manga Studio Ex 5 Cookbook
- Java編程指南:基礎(chǔ)知識、類庫應(yīng)用及案例設(shè)計
- Python爬蟲開發(fā)與項目實戰(zhàn)
- Python High Performance Programming
- 詳解MATLAB圖形繪制技術(shù)
- Django 3.0入門與實踐
- Hadoop 2.X HDFS源碼剖析
- Java 9 Programming By Example
- 零基礎(chǔ)學(xué)C語言(升級版)
- 單片機原理及應(yīng)用技術(shù)
- Delphi開發(fā)典型模塊大全(修訂版)