Commit 5e4e1967 by Patryk Czarnik

BlogService i wstrzykiwanie zależności

parent 52346d11
package org.example.demo; package org.example.demo;
import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
@RequestMapping("/blog")
public class BlogController { public class BlogController {
private List<String> teksty = Collections.synchronizedList(new ArrayList<>()); @Autowired
private BlogService blogService;
@GetMapping("/blog") @GetMapping
public String get(Model model) { public String rozmowaGet(Model model) {
model.addAttribute("teksty", teksty); model.addAttribute("teksty", blogService.getTeksty());
return "blog.html"; return "blog.html";
} }
@PostMapping("/blog") @PostMapping
public String post(Model model, String tekst) { public String rozmowaPost(String tekst, Model model) {
teksty.add(tekst); if(tekst != null && !tekst.isBlank()) {
model.addAttribute("teksty", teksty); blogService.addTekst(tekst);
}
model.addAttribute("teksty", blogService.getTeksty());
return "blog.html"; return "blog.html";
} }
} }
package org.example.demo;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/blog.json")
public class BlogRestController {
private BlogService blogService;
// innym sposobem wstrzykiwania zależności, niż @Autowired, jest utworzenie konstruktora, który wymaga podania parametru - wtedy Spring musi go dostarczyć
public BlogRestController(BlogService blogService) {
this.blogService = blogService;
}
@GetMapping
public List<String> getTeksty() {
return blogService.getTeksty();
}
@GetMapping(path="/{numer}", produces="text/plain")
public String getTekst(@PathVariable("numer") int i) {
return blogService.getTekst(i);
}
@PutMapping(path="/{numer}", consumes="text/plain")
public void setTekst(
@PathVariable("numer") int i,
@RequestBody String tekst) {
blogService.setTekst(i, tekst);
}
@PostMapping(consumes="text/plain")
public void addTekst(@RequestBody String tekst) {
blogService.addTekst(tekst);
}
}
package org.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Service;
// można też użyć adn @Component
@Service
public class BlogService {
private List<String> teksty = Collections.synchronizedList(new ArrayList<>());
public List<String> getTeksty() {
return Collections.unmodifiableList(teksty);
}
public String getTekst(int i) {
return teksty.get(i);
}
public void setTekst(int i, String tekst) {
teksty.set(i, tekst);
}
public void addTekst(String tekst) {
teksty.add(tekst);
}
}
...@@ -42,5 +42,11 @@ ...@@ -42,5 +42,11 @@
<li><a th:href="@{/kalkulator}">Kalkulator</a></li> <li><a th:href="@{/kalkulator}">Kalkulator</a></li>
</ul> </ul>
<h2>Przykład Blog</h2>
<ul>
<li><a th:href="@{/blog}">Formularz</a> do wpisywania tekstów</li>
<li><a th:href="@{/blog.json}">RESTController</a> podgląd w wersji JSON</li>
</ul>
</body> </body>
</html> </html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment