Tags: java

Mixer2をSpringBoot(SpringMVC)と組み合わせて使う場合のHelloWorld的なサンプルアプリケーションを作りました。

ついでに、maven用アーキタイプもSpringBoot1.4.xに合わせて作り直しました。

$ mvn archetype:generate \
    -DarchetypeGroupId=org.mixer2 \
    -DarchetypeArtifactId=mixer2-springmvc-archetype \
    -DarchetypeVersion=0.5.4

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Archetype repository missing. Using the one from [org.mixer2:mixer2-springmvc-archetype:0.3.2] found in catalog remote
Downloading: http://repo.maven.apache.org/maven2/org/mixer2/mixer2-springmvc-archetype/0.5.4/mixer2-springmvc-archetype-0.5.4.jar
Downloaded: http://repo.maven.apache.org/maven2/org/mixer2/mixer2-springmvc-archetype/0.5.4/mixer2-springmvc-archetype-0.5.4.jar (173 KB at 118.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/mixer2/mixer2-springmvc-archetype/0.5.4/mixer2-springmvc-archetype-0.5.4.pom
Downloaded: http://repo.maven.apache.org/maven2/org/mixer2/mixer2-springmvc-archetype/0.5.4/mixer2-springmvc-archetype-0.5.4.pom (4 KB at 7.0 KB/sec)
Define value for property 'groupId': : com.example
Define value for property 'artifactId': : foobar
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.example: :
Confirm properties configuration:
groupId: com.example
artifactId: foobar
version: 1.0-SNAPSHOT
package: com.example
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: mixer2-springmvc-archetype:0.5.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.example
[INFO] Parameter: artifactId, Value: foobar
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.example
[INFO] Parameter: packageInPathFormat, Value: com/example
[INFO] Parameter: package, Value: com.example
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.example
[INFO] Parameter: artifactId, Value: foobar
[INFO] project created from Archetype in dir: /Users/nabedge/tmp/foobar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.486 s
[INFO] Finished at: 2016-11-13T18:22:01+09:00
[INFO] Final Memory: 14M/155M
[INFO] ------------------------------------------------------------------------

こんな具合で、foobarというディレクトリ配下にfoobarというサンプルプロジェクトが出来上がります。

$ cd foobar
$ ls
pom.xml  src/

$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building foobar 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------


[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ foobar ---
[INFO] Building jar: /Users/nabedge/tmp/foobar/target/foobar-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.1.RELEASE:repackage (default) @ foobar ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

これでtarget配下にspring-bootのone-jar型式のアプリケーションが出来上がります。あとは起動するだけです。

$ java -jar ./target/foobar-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.1.RELEASE)

2016-11-13 18:32:08.807  INFO 57949 --- [           main] com.example.Application                  : Starting Application v1.0-SNAPSHOT on nabedge-no-MacBook-Air.local with PID 57949 (/Users/nabedge/tmp/foobar/target/foobar-1.0-SNAPSHOT.jar started by nabedge in /Users/nabedge/tmp/foobar)
2016-11-13 18:32:08.819  INFO 57949 --- [           main] com.example.Application                  : No active profile set, falling back to default profiles: default
2016-11-13 18:32:08.917  INFO 57949 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27f8302d: startup date [Sun Nov 13 18:32:08 JST 2016]; root of context hierarchy
2016-11-13 18:32:11.268  INFO 57949 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-11-13 18:32:11.289  INFO 57949 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-11-13 18:32:11.291  INFO 57949 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
2016-11-13 18:32:11.442  INFO 57949 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-11-13 18:32:11.443  INFO 57949 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2530 ms
2016-11-13 18:32:11.688  INFO 57949 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-11-13 18:32:11.695  INFO 57949 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-11-13 18:32:11.696  INFO 57949 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-11-13 18:32:11.696  INFO 57949 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-11-13 18:32:11.696  INFO 57949 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2016-11-13 18:32:11.794  INFO 57949 --- [           main] org.mixer2.Mixer2Engine                  : initializing mixer2 engine...
2016-11-13 18:32:17.579  INFO 57949 --- [           main] org.mixer2.Mixer2Engine                  : completed initializing mixer2 engine.
2016-11-13 18:32:17.929  INFO 57949 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27f8302d: startup date [Sun Nov 13 18:32:08 JST 2016]; root of context hierarchy
2016-11-13 18:32:18.065  INFO 57949 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.example.controller.IndexController.index(org.springframework.ui.Model)
2016-11-13 18:32:18.071  INFO 57949 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-11-13 18:32:18.072  INFO 57949 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-11-13 18:32:18.119  INFO 57949 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-13 18:32:18.119  INFO 57949 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-13 18:32:18.182  INFO 57949 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-13 18:32:18.387  INFO 57949 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-11-13 18:32:18.475  INFO 57949 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-11-13 18:32:18.482  INFO 57949 --- [           main] com.example.Application                  : Started Application in 10.53 seconds (JVM running for 11.155)

これで組み込みTomcatが8080ポートで起動しています。ブラウザで http://localhost:8080 をみてください。

201611-springmvc-archetype.png

spring-bootとMixer2を組み合わせ使う場合の最低限のコーディングの参考になると思います。Happy Hacking !