Size: 190
Comment:
|
Size: 12132
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 4: | Line 4: |
http://spring.io/guides http://spring.io/guides/gs/serving-web-content/ |
|
Line 6: | Line 10: |
=== 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 <?xml version="1.0" encoding="UTF-8"?> <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>org.springframework</groupId> <artifactId>gs-rest-service</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <start-class>hello.Application</start-class> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project> }}} 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 <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>org.springframework</groupId> <artifactId>gs-rest-service-tomcat</artifactId> <version>0.1.0</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>compile</scope> </dependency> </dependencies> <properties> <start-class>hello.Application</start-class> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project> }}} '''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<Application> 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 <?xml version="1.0" encoding="UTF-8"?> <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>org.springframework</groupId> <artifactId>gs-serving-web-content</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <properties> <start-class>hello.Application</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestone</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestone</id> <url>http://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project> }}} * 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 <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'" /> </body> </html> }}} * 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 |
Spring
Spring helps development teams everywhere build simple, portable, fast and flexible JVM-based systems and applications.
http://spring.io/guides/gs/serving-web-content/
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
<?xml version="1.0" encoding="UTF-8"?> <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>org.springframework</groupId> <artifactId>gs-rest-service</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <start-class>hello.Application</start-class> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>
src/main/java/hello/Application.java
1 package hello;
2
3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.context.annotation.ComponentScan;
6
7 @ComponentScan
8 @EnableAutoConfiguration
9 public class Application {
10
11 public static void main(String[] args) {
12 SpringApplication.run(Application.class, args);
13 }
14 }
src/main/java/hello/Greeting.java
1 package hello;
2
3 public class Greeting {
4
5 private final long id;
6 private final String content;
7
8 public Greeting(long id, String content) {
9 this.id = id;
10 this.content = content;
11 }
12
13 public long getId() {
14 return id;
15 }
16
17 public String getContent() {
18 return content;
19 }
20 }
src/main/java/hello/GreetingController.java
1 package hello;
2
3 import java.util.concurrent.atomic.AtomicLong;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestParam;
6 import org.springframework.web.bind.annotation.RestController;
7
8 @RestController
9 public class GreetingController {
10
11 private static final String template = "Hello, %s!";
12 private final AtomicLong counter = new AtomicLong();
13
14 @RequestMapping("/greeting")
15 public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
16 return new Greeting(counter.incrementAndGet(),
17 String.format(template, name));
18 }
19 }
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
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <groupId>org.springframework</groupId>
7 <artifactId>gs-rest-service-tomcat</artifactId>
8 <version>0.1.0</version>
9 <packaging>war</packaging>
10
11 <parent>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-parent</artifactId>
14 <version>1.1.5.RELEASE</version>
15 </parent>
16
17 <dependencies>
18 <dependency>
19 <groupId>org.springframework.boot</groupId>
20 <artifactId>spring-boot-starter-web</artifactId>
21 <scope>compile</scope>
22 </dependency>
23 </dependencies>
24
25 <properties>
26 <start-class>hello.Application</start-class>
27 </properties>
28 <build>
29 <plugins>
30 <plugin>
31 <artifactId>maven-compiler-plugin</artifactId>
32 <version>2.3.2</version>
33 </plugin>
34 </plugins>
35 </build>
36 <repositories>
37 <repository>
38 <id>spring-releases</id>
39 <url>http://repo.spring.io/libs-release</url>
40 </repository>
41 </repositories>
42 <pluginRepositories>
43 <pluginRepository>
44 <id>spring-releases</id>
45 <url>http://repo.spring.io/libs-release</url>
46 </pluginRepository>
47 </pluginRepositories>
48 </project>
src/main/java/hello/Application.java
1 package hello;
2
3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.context.annotation.ComponentScan;
6 import org.springframework.boot.context.web.SpringBootServletInitializer;
7 import org.springframework.boot.builder.SpringApplicationBuilder;
8 import org.springframework.context.annotation.Configuration;
9
10 @ComponentScan
11 @EnableAutoConfiguration
12 @Configuration
13 public class Application extends SpringBootServletInitializer {
14
15 public static void main(String[] args) {
16 SpringApplication.run(appClass, args);
17 }
18
19 @Override
20 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
21 return application.sources(appClass);
22 }
23
24 private static Class<Application> appClass = Application.class;
25 }
src/main/java/hello/Greeting.java
1 package hello;
2
3 public class Greeting {
4
5 private final long id;
6 private final String content;
7
8 public Greeting(long id, String content) {
9 this.id = id;
10 this.content = content;
11 }
12
13 public long getId() {
14 return id;
15 }
16
17 public String getContent() {
18 return content;
19 }
20 }
src/main/java/hello/GreetingController.java
1 package hello;
2
3 import java.util.concurrent.atomic.AtomicLong;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestParam;
6 import org.springframework.web.bind.annotation.RestController;
7
8 @RestController
9 public class GreetingController {
10
11 private static final String template = "Hello Tomcat, %s!";
12 private final AtomicLong counter = new AtomicLong();
13
14 @RequestMapping("/greetingTomcat")
15 public Greeting greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) {
16 return new Greeting(counter.incrementAndGet(), String.format(template, name));
17 }
18 }
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
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>org.springframework</groupId>
6 <artifactId>gs-serving-web-content</artifactId>
7 <version>0.1.0</version>
8 <parent>
9 <groupId>org.springframework.boot</groupId>
10 <artifactId>spring-boot-starter-parent</artifactId>
11 <version>1.1.8.RELEASE</version>
12 </parent>
13 <dependencies>
14 <dependency>
15 <groupId>org.springframework.boot</groupId>
16 <artifactId>spring-boot-starter-thymeleaf</artifactId>
17 </dependency>
18 </dependencies>
19 <properties>
20 <start-class>hello.Application</start-class>
21 </properties>
22 <build>
23 <plugins>
24 <plugin>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-maven-plugin</artifactId>
27 </plugin>
28 </plugins>
29 </build>
30 <repositories>
31 <repository>
32 <id>spring-milestone</id>
33 <url>http://repo.spring.io/libs-release</url>
34 </repository>
35 </repositories>
36 <pluginRepositories>
37 <pluginRepository>
38 <id>spring-milestone</id>
39 <url>http://repo.spring.io/libs-release</url>
40 </pluginRepository>
41 </pluginRepositories>
42 </project>
- cd src/main/java/hello/
nano GreetingController.java
1 package hello;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.ui.Model;
5 import org.springframework.web.bind.annotation.RequestMapping;
6 import org.springframework.web.bind.annotation.RequestParam;
7
8 @Controller
9 public class GreetingController {
10
11 @RequestMapping("/greeting")
12 public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
13 model.addAttribute("name", name);
14 return "greeting";
15 }
16
17 }
- nano src/main/resources/templates/greeting.html
- nano src/main/java/hello/Application.java
1 package hello;
2
3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.context.annotation.ComponentScan;
6
7 @ComponentScan
8 @EnableAutoConfiguration
9 public class Application {
10
11 public static void main(String[] args) {
12 SpringApplication.run(Application.class, args);
13 }
14
15 }
- mvn clean compile package install
- java -jar target/gs-serving-web-content-0.1.0.jar