Spring @ResponseBody Annotation
In the Spring framework, the @ResponseBody
annotation tells the Spring framework to write the method’s return type to the HTTP response body (not placed in a Model
or interpreted as a view name).
- Spring 3.0 introduced the
@ResponseBody
annotation on the method level. - Spring 4.0 enhanced the
@ResponseBody
annotation to put on the class level, and all the annotated class’s methods will inherit the@ResponseBody
. - Spring 4.0 also introduced a new
@RestController
annotation; The@RestController
annotation is a shortcut to@Controller
and@ResponseBody
.
Table of contents:
- 1. Spring @ResponseBody example
- 2. @ResponseBody and return a JSON
- 3. Spring @RestController and @ResponseBody
- 4. Download Source Code
- 5. References
1. Spring @ResponseBody example
In the below Spring MVC example, if we visit the /hello
endpoint, Spring will return an error message.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
@RequestMapping(value="/hello", method= RequestMethod.GET)
String hello() {
return "Hello World!";
}
}
We can add a @ResponseBody
annotation on the method hello()
; Since the hello()
method return type is String
, Spring will directly write the string into the HTTP response body.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping(value="/hello", method= RequestMethod.GET)
@ResponseBody
String hello() {
return "Hello World!";
}
}
Now, if we visit the /hello
endpoint, Spring will return a String "Hello World!".
% curl -i http://localhost:8080/hello
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 14
Date: Mon, 22 Aug 2022 03:34:48 GMT
Hello World
2. @ResponseBody and return a JSON
Below example creates a /json
endpoint and return an object ResponseSimpleObj
as JSON format. By default, Spring Boot adds Jackson 2 as the JSON message converter.
public class ResponseSimpleObj {
private String message;
public ResponseSimpleObj(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
// produces is optional, for object return type, default to JSON
@RequestMapping(value = "/json", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
ResponseSimpleObj json() {
return new ResponseSimpleObj("Hello World");
}
}
Output
% curl -i http://localhost:8080/json
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 22 Aug 2022 04:31:46 GMT
{"message":"Hello World"}
3. Spring @RestController and @ResponseBody
Spring 4.0 introduced a new @RestController
annotation as a shortcut for @Controller
and @ResponseBody
. We can rewrite the above JSON example with the @RestController
annotation.
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/json", method = RequestMethod.GET)
ResponseSimpleObj json() {
return new ResponseSimpleObj("Hello World");
}
}
4. Download Source Code
$ git clone https://github.com/mkyong/spring-boot.git