Spring
Spring helps development teams everywhere build simple, portable, fast and flexible JVM-based systems and applications.
REST service
spring.io/guides/gs/rest-service/
- 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
- java -jar target/gs-rest-service-0.1.0.jar