Rest Service
Simple RESTful web service with Spring on port 9090.
Use start.spring.io to create a web project.
/src/main/java/com/example/
DemoApp.java
DemoRestController.java
DemoQuote.java
Pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Demo Application
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApp {
public static void main(String[] args) {
SpringApplication.run(DemoApp.class, args);
}
}
Rest Controller
package com.example;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoRestController {
@GetMapping("/")
public String quote() {
return "Welcome!";
}
@GetMapping("/quote/{id}")
public DemoQuote getQuote(@PathVariable Integer id) {
List<DemoQuote> quotes = Arrays.asList(
new DemoQuote(1, "Spring Boot apps easy."),
new DemoQuote(2, "Spring Boot deploy everywhere.")
);
return quotes
.stream()
.filter(x -> x.getId() == id)
.collect(Collectors.toList())
.get(0);
}
}
Quote Model
package com.example;
public class DemoQuote {
private final Integer id;
private final String content;
public DemoQuote(Integer id, String content) {
this.id = id;
this.content = content;
}
public int getId() {
return id;
}
public String getContent() {
return content;
}
}
Port
Use a different port from 8080 (default).
src/main/resources/application.properties
server.port=9090
Run
mvn clean package
mvn spring-boot:run
curl http://localhost:9090/quote/1
{"id":1,"content":"Spring Boot apps easy."}
java -jar .\target\rest_service-0.0.1.jar
curl http://localhost:9090/quote/2
{"id":2,"content":"Spring Boot deploy everywhere."}
Rest Client
Rest Controller with /client_request_quote on port 9091
resources/application.properties
server.port=9091
Client Controller
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class DemoRestClientController {
@GetMapping("/client/quote/{id}")
public Object client_request_quote(@PathVariable Integer id) {
RestTemplate rt = new RestTemplate();
Object quote = rt.getForObject("http://localhost:9090/quote/" + id, Object.class);
return quote;
}
}
Run
cd rest_client/
mvn package
mvn spring-boot:run
curl http://localhost:9091/client/quote/1
{"id":1,"content":"Spring Boot apps easy."}
curl http://localhost:9091/client/quote/2
{"id":2,"content":"Spring Boot deploy everywhere."}