Sobes.tech
Junior — Middle

What needs to be done to convert an @Controller annotation to @RestController in your Spring application?

sobes.tech AI

Answer from AI

To convert the @Controller annotation to @RestController in a Spring application, simply replace the class annotation from @Controller to @RestController.

The @RestController annotation is a combination of @Controller and @ResponseBody, which means that controller methods automatically serialize the returned objects into JSON or other formats and send them in the HTTP response, without the need to explicitly specify @ResponseBody on each method.

Example:

// Was
@Controller
public class MyController {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello";
    }
}

// Now
@RestController
public class MyController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello";
    }
}
What needs to be done to convert an @Controller… - sobes.tech