Skip to main content

Command Palette

Search for a command to run...

Spring Boot

understanding of @controller and @responseBody , @controller and @requestMapping & @RestController and @requestMapping .

Published
3 min readView as Markdown
H

I am a engineering student who's determined to change the time ahead .

  1. Response Body (@RestController):

    • When you use @RestController, it implies that the controller is intended to handle RESTful web services.

    • Methods in a @RestController return data directly, which is written to the HTTP response body. This data is typically in the form of JSON or plain text.

    • The @RestController annotation itself is a shorthand for @Controller combined with @ResponseBody, meaning every method in the controller will write its return value directly to the response body.

Example:

    javaCopy code@RestController
    public class MyRestController {
        @RequestMapping("/hello")
        public String sayHello() {
            return "Hello, World!"; // This string is sent as the HTTP response body.
        }
    }
  • When you access /hello, the response body will be the plain text "Hello, World!".
  1. View Name (@Controller):

    • When you use @Controller, it implies that the controller is intended to handle web page navigation.

    • Methods in a @Controller typically return a view name. The view name is resolved by a view resolver (like Thymeleaf, JSP, etc.) to render an HTML page.

    • The @ResponseBody annotation can be used on specific methods in a @Controller to indicate that the return value should be written directly to the response body instead of being interpreted as a view name.

Example:

    javaCopy code@Controller
    public class MyWebController {
        @RequestMapping("/home")
        public String homePage() {
            return "home"; // This string is interpreted as a view name, e.g., home.jsp or home.html.
        }
    }
  • When you access /home, the view resolver looks for a view named "home" (like home.jsp or home.html) and renders it.

Practical Examples:

  1. Using @RestController:

     javaCopy codepackage com.example;
    
     import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.bind.annotation.RestController;
    
     @RestController
     public class MainRestController {
    
         @RequestMapping("/api/hello")
         public String apiHello() {
             return "Hello, REST World!"; // This will be the response body.
         }
     }
    
    • Accessing /api/hello will return "Hello, REST World!" as the HTTP response body.
  2. Using @Controller:

     javaCopy codepackage com.example;
    
     import org.springframework.stereotype.Controller;
     import org.springframework.web.bind.annotation.RequestMapping;
    
     @Controller
     public class MainWebController {
    
         @RequestMapping("/web/home")
         public String webHome() {
             return "home"; // This is a view name, such as home.jsp or home.html.
         }
     }
    
    • Accessing /web/home will direct Spring to look for a view named "home" (like home.jsp or home.html) in the configured views directory.

Combining Both (@Controller with @ResponseBody):

You can also have a @Controller and use @ResponseBody for specific methods that need to return data directly.

Example:

javaCopy codepackage com.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MixedController {

    @RequestMapping("/page/home")
    public String homePage() {
        return "home"; // Returns the view name "home".
    }

    @RequestMapping("/api/greet")
    @ResponseBody
    public String greetApi() {
        return "Greetings from API!"; // This will be the response body.
    }
}
  • /page/home: Returns the view named "home".

  • /api/greet: Returns the string "Greetings from API!" as the HTTP response body.

Summary:

  • @RestController: All methods return data directly to the HTTP response body.

  • @Controller: Methods typically return view names, which are resolved by a view resolver to render web pages.

  • @Controller with @ResponseBody: Allows specific methods to return data directly to the HTTP response body, combining both approaches.

By understanding the distinction between these two approaches, you can design your Spring Boot application to handle both RESTful APIs and traditional web page navigation effectively.

More from this blog

Harsehrawat's blog

52 posts