Projekt

Obecné

Profil

Spring REST Controller » Historie » Revize 3

Revize 2 (Jiří Trefil, 2023-03-15 21:48) → Revize 3/10 (Jiří Trefil, 2023-03-15 21:49)

h1. Spring REST Controller 

 * Controller řídí logiku toku informací aplikace s MVC architekturou, tedy řídí, jak uživatel s aplikací komunikuje. Rozhoduje, jaký response odeslat na konkrétní uživatelský request. 
 * Spring aplikace v kontextu SPADe v2 je pouze RESTové API, všechny controllery jsou tedy anotovány pomocí @RestController. Tato anotace implikuje, že třída je REST controller (správná konvence). 

 h2. Příklad REST controlleru  


 
 <pre><code class="java"> 
   @RestController 
 public class DummyController { 
     //route for Spring router - this means that this particular controller will take control on /v2/helloworld endpoint of server 
     //response body is response from server to a client  
     @RequestMapping( 
   value = "/v2/helloworld",  
   produces="application/json" 
 )  
     public String helloWorld(@RequestParam(value = "parameter", defaultValue=" World") String parameter){ 
         return "{\"message\":\"Hello "+parameter+"\"}"; 
     } 

 } System.out.println("ahoj"); 
 </code></pre>