JAVA

  minte9
REMEMBERS




Last update:   13-03-2022

Consume

The application will retrive quotes from a RESTful web service. When you request the service URL you receive a JSON document.
 
# https://quoters.apps.pcfone.io/api/random
{
   type: "success",
   value: {
      id: 10,
      quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
   }
}
Start a new Spring Boot Web project.
 
package com.example.consumingrest;
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);
    }

}
Create a domain class to contain the data that you need. Any property not bound will be ignored.
 
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties (ignoreUnknown = true) // Look Here
public class Quote {
    
    private String type;
    private Value value;

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    public Value getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Quote{type='" + type + "', value=" + value + "}";
    }
}
 
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties (ignoreUnknown = true)
public class Value {

    private Long id;
    private String quote;

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setQuote(String quote) {
        this.quote = quote;
    }

    public String getQuote() {
        return quote;
    }

    @Override
    public String toString() {
        return "Value{id='" + id + "', quote=" + quote + "}";
    }
}
A RestTemplate will use the Jackson JSON library for incomming data. A CommandLineRunner will run the RestTemplate on startup.
 
package com.example.consumingrest;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) // on startup
        throws Exception {
            return args -> {
                Quote quote = restTemplate.getForObject(
                    "https://quoters.apps.pcfone.io/api/random", Quote.class
                );
                System.out.println(quote);
            };
    }

    @GetMapping("/getquote")
    public String quote() {
        RestTemplate rt = new RestTemplate(); // on request
        Quote quote = rt.getForObject(
            "https://quoters.apps.pcfone.io/api/random", Quote.class
        );
        return quote.toString();
    }

}
Build and test the application in console or browser.
 
./mvnw spring-boot:run
 
#console
2021-09-10 18:37:21.306  INFO 3273 
Quote{type='success', value=Value{id='9', quote='Spring Boot solves ...'}

#browser
localhost:8080/getquote
Quote{type='success', value=Value{id='5', quote='Spring Boot solves ...'}

Questions    
Templates
        A B C D E F
🔔
1/1