Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate JPA in Spring Boot entities calling to an external API

Is possible to populate a database with JPA entities in Spring Boot calling an external api? If I have an entity called Quote:

package guru.springframework.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.math.BigDecimal;

@Entity
public class Quote {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long _id;
    private String description;

    public Long getId() {
        return _id;
    }

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

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

}

How Can I call this API https://quotesondesign.com/api-v4-0/ to populate my database in Spring Boot?


1 Answers

First of all i do not khnow the globale structure of your code but let me suppose :

Configt Class:

@Configuration
public class appConfig{
  @Bean
  RestTemplate restTemplate(){
    return new RestTemplate();
  }

  @Bean
  ObjectMapper objectMapper(){
    return new ObjectMapper();
  }
}

Service class:

import guru.springframework.domain.Qoate;
// Other imports

@Service
public class service{

@Autowired
private RestTemplate restTemplate;


@Autowired
private ObjectMapper objectMapper;

@Autowired
private QuoteRepository quoteRepository;

public void populateQoats {
// You can chose one of these tow cases

// Case 1
ResponseEntity<List<Qoate>> response = restTemplate.exchange(
  "http://rest to get the list of Qoates",
  HttpMethod.GET,
  null,
  new ParameterizedTypeReference<List<Qoate>>(){});
List<Qoate> result = response.getBody();

// Case 1
String response = restTemplate.getForObject(
  "http://rest to get the list of Qoates",
  String.class);

  List<Qoate> result = objectMapper.readValue(response, new TypeReference<List<Qoate>>(){});

  // Save the list into a database
  if(Objects.nonNull(result)) result.stream().filter(Objects::nonNull).foreach(element -> quoteRepository.saveAndFlush(element));

}

}

The repository:

public interface QuoteRepository  extends JpaRepository<Quote,Long>{

}

For information :

As of 5.0, the non-blocking, reactive org.springframework.web.reactive.client.WebClient offers a modern alternative to the RestTemplate with efficient support for both sync and async, as well as streaming scenarios. The RestTemplate will be deprecated in a future version and will not have major new features added going forward.

For more details

You may need to add to your appConfig.class or mainClass:

@EntityScan("paackage of your entity class")
@ComponentScan({"package of you service class","package of you controller class if you have it"})
@EnableJpaRepositories("package of your repository clas")

Finally, i hope this solve your need

like image 160
TinyOS Avatar answered Nov 26 '25 23:11

TinyOS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!