ã¡ãã£ã¨Spring Bootãå§ãã¾ãããeclipseãMersã«ãªã£ããã¨ã ãç´4å¹´ã¶ããããã®eclipseçæ´»ã§ããJavaã¯ãã¡ããJava 8ï¼Stream使ã£ã¦ãããã¨æãã¾ãã
ãã¦ãé¢ä¿ãªã話ã¯ãããããã«ãã¦Spring Bootãå§ãããã¨æãã¾ãã
ããã¸ã§ã¯ãã®ä½æ
ã¾ããããã¸ã§ã¯ãã使ãã¾ããeclipseããããã£ã¨ãMaven Projectã使ãã¾ãã
maven-archetype-quickstartãé¸ã³ï¼ããã©ã«ãã§é¸ã°ãã¦ãããã®ã§ãï¼ä½ãèããã«Nextãã¦ä½æãã¾ããä»åã¯groupIdã«hellobootããartifactIdã«sampleappãæå®ãã¾ããã
Spring Bootã®HPã«ããéãpom.xmlã«ä»¥ä¸ã®è¿½è¨ããã¾ãã
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
追è¨ããpom.xmlã¯ä»¥ä¸ã®ããã«ãªãã¾ããã
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>helloboot</groupId> <artifactId>sampleapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sampleapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Hello world
Spring Bootã®è¨å®ã¨ã¨ã³ããªãã¤ã³ãã¨ãªãAppã¯ã©ã¹ã使ãã¾ãã
package helloboot.sampleapp; 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); } }
次ã«Controllerã¯ã©ã¹ã使ãã¾ãã
package helloboot.sampleapp; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") public String sayHello() { return "Hello world"; } }
ãã®ç¶æ ã§Run AsããJava Applicationã鏿ãã¦å ã»ã©ä½æããAppã¯ã©ã¹ããèµ·åãã¾ãããã©ã¦ã¶ãç«ã¡ä¸ãã¦http://localhost:8080ã«ã¢ã¯ã»ã¹ããã¨ä»¥ä¸ã®ããã«Hello worldã表示ããã¾ãã
Thymeleafã使ã
次ã¯ååãå ¥åããããããã«ã¡ã¯ââããï¼ã¨è¡¨ç¤ºãããã©ã¼ã ã§ãä½ã£ã¦ã¿ããã¨æãã¾ããã¨ãããããSpring Bootã§ã¯Thymeleaf使ãã®ãçéï¼ã£ã½ãã®ã§ããã«ãªãã£ã¦ã¿ã¾ãã
pom.xmlã«ä»¥ä¸ã®å®ç¾©ã追å ãã¾ãã
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
src/main/resourcesã«templatesãã©ã«ããä½ã£ã¦ããã«ãã³ãã¬ã¼ããç½®ãã¾ããã¾ãã¯åä½ç¢ºèªãããã®ã§index.htmlã¨ããååã§ä»¥ä¸ã®ãã¡ã¤ã«ãç½®ãã¦ã¿ã¾ããã
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> <span th:text="${message}">Hello world</span> </body> </html>
ããã¦ãã³ã³ããã¼ã©ãViewã使ãããã«ãã¾ãã
package helloboot.sampleapp; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/") public String sayHello(Model model) { model.addAttribute("message", "ããã«ã¡ã¯ä¸ç"); return "index"; } }
å®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
å ¥åé ç®ãè¶³ãã¦ããã¾ãã
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Insert title here</title> </head> <body> <span th:text="${message}">Hello world</span> <form action="/" method="POST"> <input name="name" type="text" /><br/> <input type="submit" /> </form> </body> </html>
POSTã«å¯¾å¿ããã¡ã½ãããControllerã«è¿½å ãã¾ãã
package helloboot.sampleapp; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @RequestMapping(name = "/", method = RequestMethod.GET) public String sayHello(Model model) { model.addAttribute("message", "ããã«ã¡ã¯ä¸ç"); return "index"; } @RequestMapping(name = "/", method = RequestMethod.POST) public String createHelloMessage(@RequestParam("name") String name, Model model) { model.addAttribute("message", "ããã«ã¡ã¯" + name + "ãã"); return "index"; } }
å®è¡ããã¨å ¥åããååã表示ããã¾ãã
warãã¡ã¤ã«å
æå¾ã«warãã¡ã¤ã«ã«ãã¦ä½å¦ã«ã§ããããã¤ã§ããããã«åºãã¾ãã
pom.xmlã®packagingãjarããwarã«å¤ãã¾ãã
<packaging>war</packaging>
以ä¸ã®dependencyã追å ãã¾ãã
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
ããã¦ããµã¼ãã¬ããåæååã追å ãã¦Servletã³ã³ããã§èµ·åããã¨ãã«ã©ã®ã¯ã©ã¹ãè¨å®ãªãããã£ã¦ããã®ãæãã¦ããã¾ãã
package helloboot.sampleapp; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; public class Initializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(App.class); } }
Run AsããMaven buildãé¸ãã§packageã´ã¼ã«ãå®è¡ããã¨ä»¥ä¸ã®ããã«warãã¡ã¤ã«ãåºæ¥ã¾ããã
ã¤ãã§ãªã®ã§Azure WebAppsã«ãããã¤
以ä¸ã®ãã¼ã¸ãåèã«ãã£ã¦ã¿ã¾ããã
WebAppsã使ãã¦ãæ§æã§Javaãæå¹ã«ãã¾ãã
Java 7ãªã®ãâ¦ã¨ããã®ã¯ç½®ãã¨ãã¦FTPã§site/wwwroot/webappsã®ä¸ã®ãã¡ã¤ã«ã䏿¦åé¤ãã¦ãROOT.warã¨ããååã§warãã¡ã¤ã«ãç½®ãã¦webappsãåèµ·åããã¦URLã«ã¢ã¯ã»ã¹ããã¨ããã¦ã¾ãããï¼æ£ããæé ãã¯ç¥ããªãï¼
ãã§ãããã§ããã