The service will handle GET requests and return JSON response.
/**
* Rest Controller service
*
* Use start.spring.io to create a new Spring Web project.
* The service will handle GET requests for /greeting and return JSON response.
*
* Use start.spring.io to create a new Spring Web project.
*/package com.minte9.rest_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublicclassApp{
publicstaticvoidmain(String[] args){
SpringApplication.run(App.class, args);
}
}
Resource
In Spring RESTful web services, HTTP requests are handled by a {{resource controller`
/**
* Resource Controller - GreetingController
*
* HTTP requests are handled by a controller
* Components are identified by the @RestController annotation.
*/package com.minte9.rest_service;
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;
@RestControllerpublicclassQuoteController{
@GetMapping("/")
public String quote(){
return"Welcome! <br><br>Use /quote/{id}";
}
@GetMapping("/quote/{id}")
public Quote getQuote(@PathVariable Integer id){
List<Quote> quotes = Arrays.asList(
new Quote(1, "Spring Boot makes stand alone Spring apps easy."),
new Quote(2, "With Boot you deploy everywhere.")
);
quotes = quotes.stream()
.filter(x -> x.getId() == id)
.collect(Collectors.toList());
return quotes.get(0);
}
}
Model
To model the quote representation, create a resource representation class.