user14677978
user14677978

Reputation:

Having @Controller & @RestController only in certain method

In a spring boot application there is a contoller, some of the methods inside the controller class need to be sent to an HTML page in the templates folder through Thymeleaf, while others don't. In the first case I'd use a @Controller annotation, while in the second I'd rather use a @RestConroller annotation. But what if I have both kinds of methods?

Upvotes: 0

Views: 707

Answers (1)

Ishikawa Yoshi
Ishikawa Yoshi

Reputation: 1779

@RestController = @Controller + @ResponseBody thats only difference between them.

If you annotate your class with @Controller you also need to annotate your method with additional @ResponseBody annotation to return REST response and not annotate to use result as a View.

@Controller
public class User{

  @RequestMapping(value={"/user"})
  @ResponseBody
  public User getUser(){
    ...
  // return User object as a response application/json content type
  return user;
  }

  @RequestMapping(value={"/"})
  public String getUserPage(){
    ...
  //return View
  return "user";
  }
} 

Upvotes: 2

Related Questions