minte9
LearnRemember



Dependencies

Simple RESTful web service with Spring on port 9090.
 
<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>

Application

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;

@SpringBootApplication
public class App {

    public static void main(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;

@RestController
public class QuoteController {

    @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.
 
/**
 * Resource Representation Class - Greeting
 */

package com.minte9.rest_service;

class Quote {

    private final Integer id;
    private final String content;

    public Quote(Integer id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

Port

You might want to set a different port from 8080 for you application.
 
# src/main/resources/application.properties

server.port=9090

Run

We can now build adn run the application and test the service.
 
mvn spring-boot:run

http://localhost:9090/quote/1 # {"id":1,"content":"Spring Boot ...
http://localhost:9090/quote/2 # {"id":2,"content":"With Boot you ...




  Last update: 432 days ago