Main Tutorials

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

In the below Spring MVC example, if we visit the /hello endpoint, Spring will return an error message.

HelloController.java

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.

HelloController.java

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!".

Terminal

  % 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.

ResponseSimpleObj.java

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;
  }

}
HelloController.java

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

Terminal

  % 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.

HelloController.java

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

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments