é£è¼è¨äºãSpringBoot ã¢ããªéçºããèªã¿é²ããã¨ããµã³ãã«ã® Webã¢ããªãå¶ä½ãããã¨ãã§ãã¾ãã
ä»åã¯ãJavaã®ã³ã³ããã¼ã©ã¼ã¨ã¡ã¤ã³ã¯ã©ã¹ã使ãã¦ããã¾ãã
é£è¼è¨äº
SpringBoot ã¢ããªéçºã®é£è¼è¨äºã¯ä»¥ä¸ã®éãã§ãã
- æ¦è¦ã»ä½¿ç¨ãããã¯ã
- ããã¸ã§ã¯ã使ã»SQL使
- ã¢ãã«ã»ãªãã¸ããªã®ä½æ
- ã³ã³ããã¼ã©ã¼ã»ã¡ã¤ã³ã®ä½æï¼ä»åã®è¨äºï¼
- HTMLã»JavaScriptã®ä½æ
- CSSã»ç»åã®ä½æ
- ãã¹ãã»åä½ç¢ºèª
ç®æ¬¡
- ã³ã³ããã¼ã©ã¼ã®ä½æ
- ã¡ã¤ã³ã®ä½æ
1. ã³ã³ããã¼ã©ã¼ã®ä½æ
ãã¤ã¼ããå¦çãã RESTful API ã¨ãã¦ã以ä¸ã®ã³ã³ããã¼ã©ã¼ã使ãã¾ãã
src/main/java/sample/controller/TweetController.java
package sample.controller; import java.util.Collections; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; import sample.model.Tweet; import sample.repository.TweetRepository; @RestController @RequestMapping("/tweet") public class TweetController { @Autowired public TweetRepository repo; @PostMapping public Map<String, Tweet> create( @RequestBody Tweet tweet ) { return Collections.singletonMap( "tweet", repo.save(tweet) ); } @GetMapping public Map<String, Iterable<Tweet>> read() { return Collections.singletonMap( "tweet", repo.findAllByOrderByCreateTimeDesc() ); } @PutMapping("/{id}") public void update( @PathVariable Long id, @RequestParam String txt ) { Tweet tweet = repo.findById(id).get(); tweet.txt = txt; repo.save(tweet); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { repo.deleteById(id); } }
HTTPã¡ã½ãããURIãJavaã¡ã½ããï¼CRUDï¼ã®å¯¾å¿è¡¨ã¯ä»¥ä¸ã®éãã§ãã
| HTTPã¡ã½ãã | URI | Javaã¡ã½ãã |
|---|---|---|
| POST | /tsubuyaki | create |
| GET | /tsubuyaki | read |
| PUT | /tsubuyaki/{id} | update |
| DELETE | /tsubuyaki/{id} | delete |
URI ã® {id} ã¯å¯å¤ã§ãã¤ã¶ããã®IDã«ãªãã¾ãã
2. ã¡ã¤ã³ã®ä½æ
SpringBoot ã¢ããªã±ã¼ã·ã§ã³ãèµ·åããããã®ã¯ã©ã¹ã使ãã¾ãã
src/main/java/sample/App.java
package sample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
GitHubãªãã¸ããª
ã¢ããªã®ã³ã¼ãã¯ã以ä¸ã®ãªãã¸ããªã«ãããã¾ãã