æè¿Habréã§å ¬éãããåå¿è åãã®å€å žçãªèšäºã®1ã€ã§ãJavaã§ã®åºæ¬çãªWebã¢ããªã±ãŒã·ã§ã³ã®äœæã«ã€ããŠèª¬æãããŸããã ãã¹ãŠã¯ãµãŒãã¬ããã§å§ãŸãã次ã«JSPããŒãžãäœæããæåŸã«ãããã³ã³ããã«ãããã€ããŸããã æ°é®®ãªå€èŠ³ã§ãããèŠãŠãç§ã¯åå¿è ã®ããã ãã«ããããæãããããšã«æ°ã¥ããŸãã-ã·ã³ãã«ã§æ確ãªPHPãŸãã¯Node.jsã®èæ¯ã«å¯ŸããŠããã¹ãŠãã·ã³ãã«ã§ã-ã³ã³ãããŒã©ãäœæãããªããžã§ã¯ããè¿ããJSONãŸãã¯HTMLã«ãªããŸããã ãã®æ°æã¡ãå°ãææããããã«ããæ¥ã®æ°äººã¬ã€ãããæžãããšã«ããŸããã ãã®èšäºã®ç®çã¯ãJavaã§Webã¢ããªã±ãŒã·ã§ã³ãäœæããããšãããã«Spring Framework㧠Webã¢ããªã±ãŒã·ã§ã³ãäœæããããšã¯ãweb.xmlãpersistence.xmlãbeans.xmlãèŠåŽããŠåŒãè£ãããšã§ã¯ãªããæççãªã«ãŒãã®å®¶ãšããŠã¢ããªã±ãŒã·ã§ã³ãåéããããšã§ã¯ãªããè¿ éãã€å¿«é©ãªããã»ã¹ã èŽè¡ã¯ãåå¿è ã®éçºè ãä»ã®èšèªã®éçºè ãSpringãæé«ã®ã¿ã€ãã³ã°ã§èŠãªãã£ã人ã ã§ãã
ã¯ããã«
ãã®èšäºã§ã¯ãçŸä»£ã®Springã«å«ãŸãããã®ãWebã¢ããªã±ãŒã·ã§ã³ãéçºããããã®ããŒã«ã«ç°å¢ã®ã»ããã¢ããæ¹æ³ãããã³ããŒã¿ããŒã¹ããããŒã¿ãååŸããŠHTMLããŒãžãšJSONãæäŸããåçŽãªWebã¢ããªã±ãŒã·ã§ã³ãäœæããæ¹æ³ã«ã€ããŠèª¬æããŸãã å¥åŠãªããšã«ãç§ãæ€çŽ¢ãããã§èŠã€ããåå¿è åãã®èšäºïŒãã·ã¢èªïŒã®ã»ãšãã©ã¯ãã³ã³ããã¹ãã®æåäœæãã¢ããªã±ãŒã·ã§ã³ã®èµ·åãããã³XMLãä»ããæ§æã®äž¡æ¹ã説æããŠããŸã-ãã¡ãããããã¯çŸä»£ã®Springã§ã¯å¿ èŠãããŸããã
æ¥ãšã¯ïŒ
ãŸããããã€ãã®èšèãæ¥ãšã¯äœã§ããã ä»æ¥ããæ¥ããšããçšèªã¯ãå€ãã®å Žåããããžã§ã¯ãã®ãã¡ããªãŒå šäœãæå³ããŸãã ã»ãšãã©ã®å Žåããããã¯Pivotalãšã³ãã¥ããã£ã«ãã£ãŠéçºããã³ç£ç£ãããŠããŸãã äž»ãªïŒãã¹ãŠã§ã¯ãªãïŒSpringãã¡ããªãŒãããžã§ã¯ãã¯æ¬¡ã®ãšããã§ãã
Spring Framework ïŒãŸãã¯Spring CoreïŒ
ãã©ãããã©ãŒã ã®ã³ã¢ã¯ãã¢ããªã±ãŒã·ã§ã³ãäœæããããã®åºæ¬çãªããŒã«ãæäŸããŸã-ã³ã³ããŒãã³ã管çïŒbeansã beans ïŒãäŸåæ§æ³šå ¥ãMVCãã¬ãŒã ã¯ãŒã¯ããã©ã³ã¶ã¯ã·ã§ã³ãåºæ¬çãªããŒã¿ããŒã¹ã¢ã¯ã»ã¹ã ãããã¯äž»ã«äœã¬ãã«ã®ã³ã³ããŒãã³ããšæœè±¡åã§ãã åºæ¬çã«ä»ã®ãã¹ãŠã®ã³ã³ããŒãã³ãã§æé»çã«äœ¿çšãããŸãã
Spring MVC ïŒSpring Frameworkã®äžéšïŒ
åå¥ã«èšåãã䟡å€ããããŸãã äž»ã«Webã¢ããªã±ãŒã·ã§ã³ã«ã€ããŠèª¬æããŸãã ã³ã³ãããŒã©ããªã¯ãšã¹ããããã³ã°ãããŸããŸãªHTTPæœè±¡åãªã©ã®æŠå¿µã§åäœããŸãã Spring MVCã¯ãThymeleafãFreemakerãMustacheãªã©ã®éåžžã®ãã³ãã¬ãŒããšã³ãžã³ãçµ±åããããã«å€ãã®ãµãŒãããŒãã£ãšçµ±åãããŠããŸãã ãããã£ãŠãJSPãJSFã®ãããªææã¯å¿ èŠãããŸããã
æ¥ã®ããŒã¿
ããŒã¿ã¢ã¯ã»ã¹ïŒãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ããã³éãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ãKVã¹ãã¬ãŒãžãªã©
æ¥ã®é²
ãã€ã¯ããµãŒãã¹ã¢ãŒããã¯ãã£ã«éåžžã«åœ¹ç«ã€-ãµãŒãã¹ã®æ€åºããã¬ãŒã¹ãšèšºæãã¯ãšãªãã©ã³ãµãŒããµãŒããããã¬ãŒã«ãŒãã«ãŒã¿ãŒãªã©
æ¥ã®ã»ãã¥ãªãã£
æ¿èªãšèªèšŒãããŒã¿ãžã®ã¢ã¯ã»ã¹ãã¡ãœãããªã©ã OAuthãLDAPãããã³ããŸããŸãªãããã€ããŒã
- æ¥ã®çµ±å
ããŸããŸãªãœãŒã¹ããã®ããŒã¿ãåŠçããŸãã FTPãã1æéã«1åãã¡ã€ã«ãååŸããå¿ èŠãããå Žåã¯ããã¡ã€ã«ãè¡ã«åå²ããããããã£ã«ã¿ãŒåŠçããŠããã¥ãŒã«éä¿¡ããŸããããã¯Spring Integrationã«éä¿¡ãããŸãã
äžè¬çãªWebã¢ããªã±ãŒã·ã§ã³ã«ã¯ãSpring MVCãDataãSecurityãªã©ã®ã¹ã€ãŒããå«ãŸããå¯èœæ§ãé«ãã§ãããã 以äžã«ãããããã¹ãŠãã©ã®ããã«é£æºãããã瀺ããŸãã
ç¹ã«æ³šç®ãã¹ãã¯ã Spring Bootã§ããããã¯ã±ãŒãã®ãããããŒã§ããïŒã±ãŒãèªäœãããã ãšèãã人ãããŸãïŒãXMLæ§æã®ãã¹ãŠã®æããããåé¿ããŸãã ããŒãã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³ããã°ããäœæããã³æ§æïŒã€ãŸããã³ã³ããŒãã³ãéã®äŸåé¢ä¿ãæ§æïŒããå®è¡å¯èœãªèªå·±å®çµåã®ææç©ã«ããã¯ããããšãã§ããŸãã ããã¯ãã³ã³ããŒãã³ãã®ã»ãããå®å šãªã¢ããªã±ãŒã·ã§ã³ã«ãŸãšãããªã³ã¯ã§ãã Spring Bootã«ã€ããŠç¥ã£ãŠããã¹ãããšãããã€ããããŸãã
- ã³ãŒãçæã¯äœ¿çšããŸããã çæãããã³ãŒããããmainã¡ãœããã®ã¿ãååšããŸãã ã¢ããªã±ãŒã·ã§ã³ãçæããããã®ãŠãŒãã£ãªãã£ãæ¢ããŠããå Žå-JHipsterã®å¯èœæ§ãé«ã
- æ§æã«XMLã䜿çšããŸããã ãã¹ãŠãã¢ãããŒã·ã§ã³ãä»ããŠæ§æãããŸã
- èªåæ§æã¯æ倧éã«äœ¿çšãããŸãã Mongoã«äŸåé¢ä¿ãè¿œå ããæ¥ç¶å
ã瀺ãããŠããªãå Žå-ããŒãã¯
localhost:27017
ãè©Šè¡ãlocalhost:27017
- èšå®ã䜿çšããã³ã³ãã³ã·ã§ã³ ã ã»ãšãã©ã®æ§æã§ã¯ãäœãæ§æããå¿ èŠã¯ãããŸããã
- ç°¡åã«ç§»åããŠãããã©ã«ãã®æ§æãããªãŒããŒã©ã€ããããããšãã§ããŸãã ããšãã°ãèšå®ã§ãã¹ããæå®ããŠMongoã«æ¥ç¶ãããšã
localhost
ãèªåçã«ãããã¯ãããŸã
ç°å¢èšå®
åçŽãªã¢ããªã±ãŒã·ã§ã³ãäœæããã«ã¯ãMavenãããžã§ã¯ãããŒãããäœæããæ¹æ³ãJARãäœæãããã©ã°ã€ã³ãæ§æããæ¹æ³ãJARã«ããã¬ã€ã¢ãŠãã®çš®é¡ãSurefireãæ§æããŠãã¹ããå®è¡ããæ¹æ³ãTomcatãããŒã«ã«ã§ã€ã³ã¹ããŒã«ããã³å®è¡ããæ¹æ³ãªã©ãç¥ãå¿ èŠããããŸãã DispatcherServletã®ä»çµã¿-絶察ã«å¿ èŠãããŸããã
ææ°ã®Springã¢ããªã±ãŒã·ã§ã³ã¯ã2ã€ã®ã¹ãããã§äœæãããŸãã
- Spring Initializrã«ç§»åããŸãã
- ...ãããŠããã ãã§ãã第äºæ®µéã¯ãããŸãã
Spring Initializrã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³ã«å¿ èŠãªã³ã³ããŒãã³ãããå ¥åãããŠãSpring BootïŒInitializrã§äœæããããã¹ãŠã®ãããžã§ã¯ãã«èªåçã«å«ãŸããŸãïŒããŸãšããããšãã§ããŸãã
äŸãã°ãç¡æã®IntelliJ IDEA CEã¯åªããä»äºãããŸã-äœæãããpom.xmlïŒMavenïŒãŸãã¯build.gradleïŒGradleïŒãã¡ã€ã«ãIDEã«ã€ã³ããŒãããã ãã§ãã
DevToolsãšåŒã°ããSpring Bootã³ã³ããŒãã³ããåå¥ã«æ³šç®ãã䟡å€ããããŸãã 以åã¯æ¬¡ã®ããã«èŠããŠããããŒã«ã«éçºãµã€ã¯ã«ã®åé¡ã解決ããŸãã
- ããŒã«ã«ã§WARãæ§ç¯ãã
- OKEASãããŒã«ã«ã§èšå®ããã«ã¯ããªãã®ã¹ãã«ãå¿ èŠãªããããã¹ããµãŒããŒã®Very Cool Enterprise Application Serverã§ãããæŒãäžããŸãã
- äžæ¯ã®ã³ãŒããŒã飲ã OKEASã¯ããããæ°åã§ã¢ããªã±ãŒã·ã§ã³ãåèµ·åããŸã
- ã¹ã¿ãã¯ãšã©ãŒã®ã³ããŒ
- ã¹ããã1ã«é²ã¿ãŸã
å€ä»£ã§ã¯ãSpringã¯XMLæ§æãã¹ã¿ãã¯ãã¬ãŒã¹ã«å€æããããã®DSLã§ãããšããæ ŒèšããçãŸããŸããã
Spring Boot DevToolsãæå¹ã«ãããš ãéçºãµã€ã¯ã«ã¯æ¬¡ã®ããã«ççž®ãããŸãã
- IDEAã®ç·è²ã®äžè§åœ¢ããã¢ããªã±ãŒã·ã§ã³ãèµ·åããŸã
DevToolsã¯ãã³ã³ãã€ã«ãããã³ãŒããŸãã¯ãã³ãã¬ãŒãã®å€æŽãèªåçã«ãã§ãã¯ããã¢ããªã±ãŒã·ã§ã³ã®ãæŠéãéšåïŒnode.jsã®äžçã«ç²ŸéããŠããå Žåã¯nodemonãªã©ïŒã®ã¿ãéåžžã«è¿ éã«åèµ·åïŒããããªããŒãïŒããŸãã ããã«ãDevToolsã«ã¯Live Reloadãšã®çµ±åãå«ãŸããŠããããã©ãŠã¶ã«æ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããåŸãIDEAã§ãããžã§ã¯ããã³ã³ãã€ã«ããã°ããã©ãŠã¶ã§èªåçã«æŽæ°ãããŸãã
éçº
ããŠãå®çšçãªéšåãéå§ããæãæ¥ãŸããã ãã®ãããç§ãã¡ã®ç®æšã¯ããŠã§ã«ã«ã ããŒãžãæäŸããããããç¬èªã®APIã«ã¢ã¯ã»ã¹ããããŒã¿ããŒã¹ããããŒã¿ãå«ãJSONãåä¿¡ããããŒãã«ã«è¡šç€ºããWebã¢ããªã±ãŒã·ã§ã³ãäœæããããšã§ãã
æ°ãããããžã§ã¯ã
éå§ããã«ã¯ã start.spring.ioã«ç§»åãã Web ã DevTools ã JPA ïŒãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ãžã®ã¢ã¯ã»ã¹ïŒã H2 ïŒã·ã³ãã«ãªã€ã³ã¡ã¢ãªããŒã¿ããŒã¹ïŒã Mustache ïŒãã³ãã¬ãŒããšã³ãžã³ïŒã®äŸåé¢ä¿ãæã€ãããžã§ã¯ããäœæããŸãã çæãããpom.xml
IDEAã«ã€ã³ããŒããããŸãã ãã¹ãŠãã¢ããªã±ãŒã·ã§ã³ãèµ·åããæºåãã§ããŸããïŒ ã³ãã³ã./mvnw spring-boot:run
䜿çšããŠã³ãã³ãã©ã€ã³ãã./mvnw spring-boot:run
ããmainã¡ãœãããå®è¡ããŠIDEAããçŽæ¥./mvnw spring-boot:run
ã ã¯ããã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãã³ã³ãããŒãããã³å±éã¯å¿
èŠãããŸããã
ããæ£ç¢ºã«ã¯ãã³ã³ãããå¿ èŠã§ã-çµã¿èŸŒã¿ã®Tomcatã䜿çšããŠãSpring Bootã«ãã£ãŠæäŸããã³èšå®ãããã ãã§ã
ã³ã³ãããŒã©ãŒ
ãããã£ãŠã次ã®ã¹ãããã¯ãã³ã³ãããŒã©ãŒãäœæããŠãããŒã ãããŒãžã«æ»ãããšã§ãã ã³ã³ãããŒã©ãŒã®ã³ãŒãã¯äºæ³éãç°¡åã§ãïŒ
@Controller public class IndexController { @GetMapping("/") public ModelAndView index() { Map<String, String> model = new HashMap<>(); model.put("name", "Alexey"); return new ModelAndView("index", model); } }
泚ç®ã«å€ããããã€ãã®ããšã
- ãã®ã¡ãœããã¯
ModelAndView
è¿ããŸãModelAndView
ã¯ãresources/templates
ãã©ã«ããŒïŒãããããã©ã«ãã®åæã§ãïŒããindex.html
resources/templates
ãååŸããããã«ã¢ãã«ãæž¡ãå¿ èŠãããããšãç¥ã£ãŠããŸã - ãã®å Žåã®ã¢ãã«ã¯åãªãèŸæžã§ããã匷ãåä»ããããã¢ãã«ïŒãªããžã§ã¯ãïŒã§ãããŸããŸãã
Kotlinã®å Žåãããã¯ããã«è¯ãã·ã³ãã«ã«èŠããã¯ãã§ãããèšèªããã¬ãŒã ã¯ãŒã¯ãªã©ãå€æ°ã®æ°ããæŠå¿µãäžåºŠã«å°å ¥ããå¿ èŠããããŸãã å°ããå§ããæ¹ãè¯ãã
@Controller
ãšããŠããŒã¯ãããã¯ã©ã¹ã¯MVCã«ãŒã¿ãŒã«èªåçã«ç»é²ããã @(Get|Post|Put|Patch)Mapping
ã¢ãããŒã·ã§ã³ã䜿çšããŠãç°ãªããã¹ãç»é²ã§ããŸãã
resources/static/
ãã£ã¬ã¯ããªã®ãã¹ãŠã®ãã¡ã€ã«ã¯éçãšèŠãªãããCSSãšç»åãä¿åã§ããŸãã
æš¡æ§
MoustacheïŒãã³ãã«ããŒïŒæ§æã䜿çšããŠããããããã³ãã¬ãŒãã¯éåžžã®HTMLã«éåžžã«äŒŒãŠããŸã
<!DOCTYPE html> <html lang="en"> <body> <h1>Welcome to Spring, {{ name }}</h1> </body> </html>
ãããžã§ã¯ããã³ã³ãã€ã«ããåŸïŒâ/ Ctrl + F9ïŒ-ããã«http://localhost:8080
ããŠãäœæãããããŒãžã確èªã§ããŸãã
ããŒã¹ãžã®ã¢ã¯ã»ã¹
ãŸãã察象åéã«ã€ããŠèª¬æããŸãã 蚪åã®çµ±èšãåéããŸã-誰ããã¡ã€ã³ããŒãžã«ã¢ã¯ã»ã¹ãããã³ã«ãããŒã¿ããŒã¹ã«ãããæžã蟌ã¿ãŸãã ã¢ãã«ã¯éåžžã«åå§çã«èŠããŸãïŒ
@Entity public class Visit { @Id @GeneratedValue public Long id; public String description; }
äžé£ã®ã³ã¡ã³ããã²ãã¿ãŒãšã»ãã¿ãŒãªãã§ã¯ããããªãããšãã€ã³ãŒã«/ããã·ã¥ã³ãŒãã¯ã©ãã«ããã®ãããèŠè¶ããŠ-ãããã®èŠçŽ ã¯ã³ãŒããåçŽåããããã«æå³çã«èŠéãããŠããŸãã ãã®ãã³ã»ã³ã¹ïŒã²ãã¿ãŒãšæ¯èŒã¡ãœããïŒãæžãããšã匷å¶ããJavaã®èšèšã®çµ¶å¯Ÿã«å·šå€§ãªãšã©ãŒãããã¯ããã¡ãããå¥åã®äŒè©±ã§ãã ãšããã§ãKotlinã¯ãã®åé¡ã解決ããŸãã
åã³ã¢ãããŒã·ã§ã³ãéåžžã«ç©æ¥µçã«äœ¿çšããŸã-ä»åã¯Spring Dataããã§ãïŒããæ£ç¢ºã«ã¯ãJPAã¯ããŒã¿ã«ã¢ã¯ã»ã¹ããããã®å¯ãªä»æ§ã§ãïŒã ãã®ã¯ã©ã¹ã¯ã1ã€ãèªåçã«çæããã2ã€ã®ãã£ãŒã«ããæã€ã¢ãã«ãèšè¿°ããŸãã ãã®ã¯ã©ã¹ã®å ŽåãããŒã¿ããŒã¹å ã®ããŒã¿ã¢ãã«ïŒããŒãã«ïŒãèªåçã«äœæãããŸãã
次ã«ããã®ã¢ãã«ã®ãªããžããªãäœæããŸãã ããã¯ã³ã³ãããŒã©ãŒãããç°¡åã§ãã
@Repository public interface VisitsRepository extends CrudRepository<Visit, Long> { }
ããã ãã§ãããªããžããªã䜿çšããŠããŒã¿ããŒã¹ãæäœã§ããŸã-ã¬ã³ãŒãã®èªã¿åããšæžã蟌ã¿ã 泚ææ·±ãèªè ã¯ãWTFãã£ãã¯ã¿ãŒãããªã¬ãŒããå¿ èŠããããŸãã ã€ã³ã¿ãŒãã§ãŒã¹ãå®çŸ©ãããšãçªç¶ããŒã¿ããŒã¹ã®æäœãéå§ãããŸããïŒ ããã ãã§ã ã å éš ãã®Spring BootãšSpring Dataã®éæ³ã®ãããã§ã次ã®ããšãèµ·ãããŸãã
- H2äŸåé¢ä¿ïŒåã蟌ã¿ããŒã¿ããŒã¹ïŒã確èªãããšãBootã¯èªåçã«
DataSource
ïŒããã¯ããŒã¿ããŒã¹ã«æ¥ç¶ããããã®éèŠãªã³ã³ããŒãã³ãã§ãïŒãæ§æããã¢ããªã±ãŒã·ã§ã³ããã®ããŒã¿ããŒã¹ã§åäœããããã«ããŸã - Spring Dataã¯
CrudRepository
ãã¹ãŠã®åå«ãæ€çŽ¢ãããããã®ããã©ã«ãå®è£ ãèªåçã«çæããŸããããã«ã¯ãfindOne
ãfindAll
ãsave
ãªã©ã®ãªããžããªã®åºæ¬ã¡ãœãããå«ãŸãsave
ã - Springã¯èªåçã«ããŒã¿ã¢ã¯ã»ã¹å±€ãæ§æããŸã-JPA ïŒããæ£ç¢ºã«ã¯ã Hibernateã®å®è£ ïŒ
-
@Repository
ã¢ãããŒã·ã§ã³ã®ãããã§@Repository
ãã®ã³ã³ããŒãã³ãã¯ã¢ããªã±ãŒã·ã§ã³ã§å©çšå¯èœã«ãªããŸãïŒæ°åã§äœ¿çšããŸãïŒ
ã³ã³ãããŒã©ãŒã§ãªããžããªãŒã䜿çšããã«ã¯ãSpring FrameworkãæäŸããäŸåæ§æ³šå ¥ã¡ã«ããºã ã䜿çšããŸãã ãããè¡ãã«ã¯ãå¥åŠãªããšã«ãã³ã³ãããŒã©ãŒã§äŸåé¢ä¿ã宣èšããã ãã§ãã
@Controller public class IndexController { final VisitsRepository visitsRepository; public IndexController(VisitsRepository visitsRepository) { this.visitsRepository = visitsRepository; } ... }
ã³ã³ã¹ãã©ã¯ã¿ãŒã§VisitRepository
åã®ãã©ã¡ãŒã¿ãŒãèŠãåŸãSpringã¯Spring Dataã«ãã£ãŠäœæããããªããžããªãŒãèŠã€ããŠãã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ããŸãã
ããã§ãã³ã³ãããŒã©ãŒã¡ãœããã§ããŒã¿ããŒã¹ã«æžã蟌ãããšãã§ããŸãã
@GetMapping("/") public ModelAndView index() { Map<String, String> model = new HashMap<>(); model.put("name", "Alexey"); Visit visit = new Visit(); visit.description = String.format("Visited at %s", LocalDateTime.now()); visitsRepository.save(visit); return new ModelAndView("index", model); }
RESTã³ã³ãããŒã©ãŒ
次ã®ã¹ãããã§ã¯ãããŒã¿ããŒã¹ãããã¹ãŠã®ã¬ã³ãŒããJSON圢åŒã§è¿ããåŸã§ã¯ã©ã€ã¢ã³ãã§èªã¿åããããã«ããŸãã
RESTã®å ŽåãSpringã«ã¯@RestController
ãšåŒã°ããå¥ã®ã³ã³ãããŒã©ãŒã¿ã€ãã@RestController
ããã®ã³ãŒãã¯éåžžã®ã³ã³ãããŒã©ãŒãšããã»ã©å€ãããŸããã
@RestController @RequestMapping("/api") public class ApiController { final VisitsRepository visitsRepository; public ApiController(VisitsRepository visitsRepository) { this.visitsRepository = visitsRepository; } @GetMapping("/visits") public Iterable<Visit> getVisits() { return visitsRepository.findAll(); } }
æ¢ãã¹ããã®ïŒ
- ä»åã¯ãåºæ¬çãª
@RequestMapping
ã䜿çšããŠããã¹ãŠã®ã³ã³ãããŒã©ãŒã¡ãœããã®ããã¬ãã£ãã¯ã¹ããå®çŸ©ããŸã - äŸåæ§æ³šå ¥ã¯ãéåžžã®ã³ã³ãããŒã©ãŒïŒSpringã®ãã¹ãŠã®ã³ã³ãããŒã©ãŒïŒãšãŸã£ããåãããã«æ©èœããŸãã
- ã¡ãœããã¯ããã³ãã¬ãŒãã®ååã§ã¯ãªããã¢ãã«ãè¿ãããã«ãªããŸããã Springã¯ãããJSONãªããžã§ã¯ãã®é åã«èªåçã«å€æããŸã
- JSONã§ã®ã·ãªã¢ã«åã«ã¯æ°žç¶ã¢ãã«ã䜿çšããŸãããããã¯äžè¬çã«ãã¹ããã©ã¯ãã£ã¹ã§ã¯ãããŸããã
ããã§ã http://localhost:8080/api/visits
ããªã¯ãšã¹ããããšãïŒãããžã§ã¯ããã³ã³ãã€ã«ãã DevToolsã«ã¢ããªã±ãŒã·ã§ã³ãæŽæ°ãããåŸïŒãå¿
èŠãªããŒã¿ãå«ãJSONãååŸããŸãã
ã¯ã©ã€ã¢ã³ãã³ãŒã
ãã®èšäºã®ç¯å²ãè¶ ããŠããœãŒã¹ã³ãŒãã§äŸãèŠãããšãã§ããŸãã ãã®ã³ãŒãã®ç®çã¯ããµãŒããŒããJSONããŒã¿ãååŸããæ¹æ³ãæä»çã«ç€ºãããšã§ããReactã¯ã©ã€ã¢ã³ããã¬ãŒã ã¯ãŒã¯ãšã®çµ±åãAngularãªã©ã¯ããã®èšäºã®ç¯å²å€ã§ãã
ãã¹ãäž
Springã¯ã çµ±åããã³åäœã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãã®ããã®åŒ·åãªããŒã«ãæäŸããŸãã ã³ã³ãããŒã©ãŒããã§ãã¯ãããµã³ãã«ã³ãŒãïŒ
@Test public void indexControllerShouldReturnHtmlPage() throws Exception { mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(content().string(containsString("Welcome to Spring"))); }
MockMvc
ãããªæœè±¡åã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³ã®å€éšã€ã³ã¿ãŒãã§ã€ã¹ãç°¡åã«ãã¹ãã§ããåæã«å
éšã«ã¢ã¯ã»ã¹ã§ããŸãã ããšãã°ãã¢ããªã±ãŒã·ã§ã³ã³ã³ããŒãã³ããmokiïŒã¹ã¿ãïŒã«å®å
šã«çœ®ãæããããšãã§ããŸãã
åæ§ã«ãAPIãã¹ãã«ã¯ã JsonPathåŒããã§ãã¯ããããã®ãã«ããŒã®ã»ããããããŸãã
@Test public void apiControllerShouldReturnVisits() throws Exception { mockMvc.perform(get("/")); mockMvc.perform(get("/api/visits")) .andExpect(jsonPath("$.*.description", iterableWithSize(1))); }
Springã§ã®ãã¹ãã¯ãŸã å¥ã®ãããã¯ã§ãããããããã§ã¯è©³ãã説æããŸããã
å±é
æ¬çªç°å¢ã§ã¢ããªã±ãŒã·ã§ã³ããã«ãããŠå®è¡ããã«ã¯ãããã€ãã®ãªãã·ã§ã³ããããŸãã
- Tomcatãªã©ã®ãµãŒãã¬ããã³ã³ããã§ãçµæã®JARïŒãŸãã¯WARïŒãæŒãäžããŸãã ããã¯æãç°¡åãªæ¹æ³ã§ã¯ãããŸãã;åäœãããµãŒãã¬ããã³ã³ãããŸãã¯ã¢ããªã±ãŒã·ã§ã³ãµãŒããŒãæ¢ã«ããå Žåã«ã®ã¿éžæããå¿ èŠããããŸãã
- Spring Bootã®éæ³ã䜿çšããŠãã ããã Spring Bootãã©ã°ã€ã³ïŒSpring Initializrã§äœæããããããžã§ã¯ãã«èªåçã«è¿œå ãããïŒã䜿çšããŠã³ã³ãã€ã«ãããJARãã¡ã€ã«ã¯å®å šã«èªå·±å®çµåã§ãã
ãããã£ãŠãã¢ããªã±ãŒã·ã§ã³ã®ã¢ã»ã³ããªãšèµ·åã¯æ¬¡ã®ããã«ãªããŸãã
-
./mvnw package
-
java -jar ./target/demo-0.0.1-SNAPSHOT.jar
ãã®JARãã¡ã€ã«ããããã€ããã«ã¯ãã€ã³ã¹ããŒã«æžã¿ã®JavaïŒJREïŒã®ã¿ãå¿ èŠã§ãã ããã¯ããããããã¡ããJARã§ããçµã¿èŸŒã¿ã®ãµãŒãã¬ããã³ã³ããïŒããã©ã«ãã§ã¯TomcatïŒãšãã¬ãŒã ã¯ãŒã¯ã®äž¡æ¹ãããã³ãã¹ãŠã®äŸåã©ã€ãã©ãªãå«ãŸããŠããŸãã å®éãããã¯å¯äžã®ãããã€ã¡ã³ãã¢ãŒãã£ãã¡ã¯ãã§ããã¿ãŒã²ãããµãŒããŒã«åçŽã«ã³ããŒããŠå®è¡ã§ããŸãã
ããã«ããã¡ã€ã«ããå®è¡å¯èœãã«ããŠãã³ãã³ãã©ã€ã³ããç°¡åã«å®è¡ããããšãã§ããŸãïŒãã¡ãããJavaãå¿ èŠã§ãïŒã
ãã®ãã¡ã€ã«ã«åºã¥ããŠãDockerã€ã¡ãŒãžãç°¡åã«äœæããããããŒã¢ã³ãšããŠã€ã³ã¹ããŒã«ãããã§ããŸãã 詳现ã¯å ¬åŒããã¥ã¡ã³ãã«èšèŒãããŠããŸã ã
ãããã«
ããã«ãããããããéåžžã«ç°¡æœã«å€æããŸãã-ããããæãç°¡åãªSpringã®å ¥éã³ãŒã¹ã§ããã1ã€ã®èšäºã®ãã¬ãŒã ã¯ãŒã¯ã«å ¥ããããšã¯éåžžã«ç°¡åã§ã¯ãããŸããã ããã誰ããæ¥ã®æåã®äžæ©ãèžã¿åºãã®ã«åœ¹ç«ã€ããšãé¡ã£ãŠããŸããã圌ã®åºæ¬æŠå¿µã¯ç解ããŠããŸãã
ã芧ã®ãšããããã®èšäºã®æ¬æã§ãSpring MagicããšããèšèãäœåºŠãèãããŠããŸãã åºæ¬çã«ãããã¯éåžžã«ãéæ³ã®ããã¬ãŒã ã¯ãŒã¯ã§ã-æ°·å±±ã®æäžéšãèŠãŠãããSpringãå€ãã®ããšãããã¯ã°ã©ãŠã³ãã§å®è¡ããŠããããšãæ¢ã«èŠãŸããã ããã¯ããã¬ãŒã ã¯ãŒã¯ã®ãã©ã¹ãšãã€ãã¹ã®äž¡æ¹ã§ãã ããã«ãééããªããå€ãã®è€éãªïŒéåžžã«å€ãã®ïŒãã®ã1ã€ã®æ³šéãŸãã¯äŸåé¢ä¿ã§äœæã§ããŸãã æ¬ ç¹ã¯é ããè€éãã§ããè€éãªåé¡ã解決ãããã極端ãªå Žåã«ãã¬ãŒã ã¯ãŒã¯ãåäœããããããã¹ãŠã®åŸ®åŠãªç¹ãåŽé¢ãç解ãããããã«ã¯ããããããç¥ãå¿ èŠããããŸãã
ãç¥ãã段éãã§ããã ãç°¡åã«ããããã«ãSpringã«ã¯åªããããã¥ã¡ã³ãã巚倧ãªã³ãã¥ããã£ãã¯ãªãŒã³ã§èªã¿ããããœãŒã¹ããããŸãã SpringãRich Hickeyã¹ã±ãŒã«ã«é 眮ãããšãSpringïŒSpringïŒã¯ç¢ºãã«ç°¡åã«ãªããŸãããåçŽã§ã¯ãããŸããã ããããïŒäŒæ¥ã ãã§ãªãïŒçŸä»£ã®äŒæ¥ã«ãšã£ãŠã¯ãæ¬çªç°å¢ã«å¯Ÿå¿ããã¢ããªã±ãŒã·ã§ã³ãéåžžã«è¿ éã«å ¥æããåšå²ã®ã€ã³ãã©ã¹ãã©ã¯ãã£ã§ã¯ãªãã¢ããªã±ãŒã·ã§ã³ããžãã¯ã«éäžãããšããçŽ æŽãããæ©äŒãæäŸããŸãã
åç §è³æ
- GithubãœãŒã¹ã³ãŒã
- ããŸããŸãªspring.io ã¬ã€ããå«ãSpringã¡ã€ã³ãµã€ã
- Spring Frameworkãã¯ã€ãããŒããŒ