Difference between Thymeleaf @RestController and @Controller

Overview

A beginner working with Thymeleaf wanted to know the difference in behavior between @Controller and @RestController, so I investigated it.

environment

Introduction

Start sprintboot and access the following URL. http://localhost:8080/〇〇

@ Controller

With the following code http://localhost:8080/list When you access main/resources/templates/list_display.html The html of is displayed on the screen. (The character string "list_display" is not displayed as it is)

@Controller
public class AppController {
    @GetMapping("list")
    public String from_list(){
        return "list_display";
    }
}

@ RestController

On the other hand, create the following controller file and use @RestController to create the following controller file. http://localhost:8080/person When you access

@RestController
public class SampleController {
    @GetMapping("person")
    public Person person() {
        return new Person(123, "hogehoge", 40);
    }
}

The following string is returned as is. {"id":123,"name":"hogehoge","age":40}

(Create a separate Person class like the one below)


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Person {
    Integer id;
    String name;
    Integer age;
}

Summary

--If you want to display the html file in resource / on the screen, @RestController --If you just want to output a character string, @Controller

Let's use.

――It's easy, but I've put it together for my memorandum.

Recommended Posts

Difference between Thymeleaf @RestController and @Controller
Difference between vh and%
Difference between i ++ and ++ i
Difference between product and variant
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Relationship between Controller and View
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
Difference between new and create in Rais action controller
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between addPanel and presentModally of FloatingPanel
[Ruby] Difference between print, puts and p
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Rails] Difference between redirect_to and render [Beginner]
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds