Spring REST Controller » Historie » Verze 3
Jiří Trefil, 2023-03-15 21:49
1 | 1 | Jiří Trefil | h1. Spring REST Controller |
---|---|---|---|
2 | 2 | Jiří Trefil | |
3 | * 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. |
||
4 | * 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). |
||
5 | 3 | Jiří Trefil | |
6 | h2. Příklad REST controlleru |
||
7 | |||
8 | |||
9 | 1 | Jiří Trefil | <pre><code class="java"> |
10 | 3 | Jiří Trefil | @RestController |
11 | public class DummyController { |
||
12 | //route for Spring router - this means that this particular controller will take control on /v2/helloworld endpoint of server |
||
13 | //response body is response from server to a client |
||
14 | @RequestMapping( |
||
15 | value = "/v2/helloworld", |
||
16 | produces="application/json" |
||
17 | ) |
||
18 | public String helloWorld(@RequestParam(value = "parameter", defaultValue=" World") String parameter){ |
||
19 | return "{\"message\":\"Hello "+parameter+"\"}"; |
||
20 | } |
||
21 | |||
22 | } |
||
23 | 2 | Jiří Trefil | </code></pre> |