= Spring = Spring helps development teams everywhere build simple, portable, fast and flexible JVM-based systems and applications. http://spring.io/guides http://spring.io/guides/gs/serving-web-content/ http://spring.io/guides/gs/convert-jar-to-war-maven/ == REST service == spring.io/guides/gs/rest-service/ === Structure === {{{ . |-- pom.xml |-- src | `-- main | `-- java | `-- hello | |-- Application.java | |-- Greeting.java | `-- GreetingController.java `-- target |-- classes | `-- hello | |-- Application.class | |-- Greeting.class | `-- GreetingController.class |-- generated-sources | `-- annotations |-- gs-rest-service-0.1.0.jar |-- gs-rest-service-0.1.0.jar.original `-- maven-archiver `-- pom.properties }}} === Steps === * mkdir -p /tmp/greetingSpring * mkdir -p /tmp/greetingSpring/src/main/java/hello * mkdir -p /tmp/greetingSpring/target * cd /tmp/greetingSpring pom.xml {{{#!highlihht xml 4.0.0 org.springframework gs-rest-service 0.1.0 org.springframework.boot spring-boot-starter-parent 1.1.5.RELEASE org.springframework.boot spring-boot-starter-web hello.Application maven-compiler-plugin 2.3.2 org.springframework.boot spring-boot-maven-plugin spring-releases http://repo.spring.io/libs-release spring-releases http://repo.spring.io/libs-release }}} src/main/java/hello/Application.java {{{#!highlight java package hello; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } }}} src/main/java/hello/Greeting.java {{{#!highlight java package hello; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } } }}} src/main/java/hello/GreetingController.java {{{#!highlight java package hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting") public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } } }}} Build with * mvn clean compile package Run with: * jar -tf target/gs-rest-service-0.1.0.jar # check JAR contents * java -jar target/gs-rest-service-0.1.0.jar * open URL http://localhost:8080/greeting == Tomcat REST service == Structure {{{ . |-- pom.xml |-- src `-- main `-- java `-- hello |-- Application.java |-- Greeting.java `-- GreetingController.java }}} '''pom.xml''' {{{#!highlight xml 4.0.0 org.springframework gs-rest-service-tomcat 0.1.0 war org.springframework.boot spring-boot-starter-parent 1.1.5.RELEASE org.springframework.boot spring-boot-starter-web compile hello.Application maven-compiler-plugin 2.3.2 spring-releases http://repo.spring.io/libs-release spring-releases http://repo.spring.io/libs-release }}} '''src/main/java/hello/Application.java''' {{{#!highlight java package hello; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Configuration; @ComponentScan @EnableAutoConfiguration @Configuration public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(appClass, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(appClass); } private static Class appClass = Application.class; } }}} '''src/main/java/hello/Greeting.java''' {{{#!highlight java package hello; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } } }}} '''src/main/java/hello/GreetingController.java''' {{{#!highlight java package hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private static final String template = "Hello Tomcat, %s!"; private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greetingTomcat") public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } } }}} === Build and deploy === * mvn clean compile package install * cp target/gs-rest-service-tomcat-0.1.0.war /usr/local/tomcat/webapps/ * cp target/gs-rest-service-tomcat-0.1.0.war /opt/apache-tomcat-7.0.53/webapps/ # in alternative * Open http://localhost:8080/gs-rest-service-tomcat-0.1.0/greetingTomcat == Spring MVC == '''Structure:''' {{{ . |-- pom.xml |-- src `-- main |-- java | `-- hello | |-- Application.java | `-- GreetingController.java `-- resources `-- templates `-- greeting.html }}} * mkdir -p src/main/java/hello * mkdir -p src/main/resources/templates/ * nano pom.xml {{{#!highlight xml 4.0.0 org.springframework gs-serving-web-content 0.1.0 org.springframework.boot spring-boot-starter-parent 1.1.8.RELEASE org.springframework.boot spring-boot-starter-thymeleaf hello.Application org.springframework.boot spring-boot-maven-plugin spring-milestone http://repo.spring.io/libs-release spring-milestone http://repo.spring.io/libs-release }}} * cd src/main/java/hello/ * nano GreetingController.java {{{#!highlight java package hello; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class GreetingController { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; } } }}} * nano src/main/resources/templates/greeting.html {{{#!highlight html Getting Started: Serving Web Content

}}} * nano src/main/java/hello/Application.java {{{#!highlight java package hello; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } }}} * mvn clean compile package install * java -jar target/gs-serving-web-content-0.1.0.jar * Open http://localhost:8080/greeting?name=Yooo == testSpringMVCJSP == Structure Maven: {{{ . |-- pom.xml |-- src | `-- main | |-- java | | `-- org | | `-- allowed | | `-- bitarus | | `-- hello | | `-- HelloController.java | `-- webapp | `-- WEB-INF | |-- mvc-dispatcher-servlet.xml | |-- pages | | `-- greeting.jsp | `-- web.xml }}} pom.xml {{{ 4.0.0 org.allowed.bitarus testSpringMVCJSP 0.1.0 war org.springframework spring-core 4.1.4.RELEASE org.springframework spring-web 4.1.4.RELEASE org.springframework spring-webmvc 4.1.4.RELEASE }}} HelloController.java {{{ package org.allowed.bitarus.hello; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @RequestMapping("/hello") public String hellox(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; } } }}} mvc-dispatcher-servlet.xml {{{ /WEB-INF/pages/ .jsp }}} greeting.jsp {{{ Getting Started: Serving Web Content

Hello ${name}

}}} web.xml {{{ Spring MVC Application + JSP mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher / contextConfigLocation /WEB-INF/mvc-dispatcher-servlet.xml org.springframework.web.context.ContextLoaderListener }}} * mvn clean compile package * Access in Tomcat http://localhost:8081/testSpringMVCJSP-0.1.0/hello?name=asd == testSpringThymeleaf ( Spring MVC + Thymeleaf + Glassfish / JBoss )== Maven Structure {{{ . |-- pom.xml |-- src | `-- main | |-- java | | `-- org | | `-- allowed | | `-- bitarus | | `-- hello | | |-- HelloController.java | `-- webapp | `-- WEB-INF | |-- mvc-dispatcher-servlet.xml | |-- pages | | |-- greeting.html | |-- web.xml }}} pom.xml {{{#!highlight xml 4.0.0 org.allowed.bitarus testSpringMVCThymeleaf 0.1.0 war org.springframework spring-core 4.1.4.RELEASE org.springframework spring-web 4.1.4.RELEASE org.springframework spring-webmvc 4.1.4.RELEASE org.thymeleaf thymeleaf-spring4 2.1.4.RELEASE org.slf4j slf4j-api 1.7.10 org.slf4j slf4j-log4j12 1.7.10 }}} HelloController.java {{{#!highlight java package org.allowed.bitarus.hello; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @RequestMapping("/hello") public String hellox(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); return "greeting"; } } }}} mvc-dispatcher-servlet.xml {{{#!highlight xml }}} greeting.html {{{#!highlight html Getting Started: Serving Web Content

}}} web.xml {{{#!highlight xml Spring MVC Application + Thymeleaf mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher / contextConfigLocation /WEB-INF/mvc-dispatcher-servlet.xml org.springframework.web.context.ContextLoaderListener }}} * mvn clean compile package * Access in Tomcat http://localhost:8081/testSpringMVCThymeleaf-0.1.0/hello?name=agh