Commit 7dc9adcc authored by Lokesh Singh's avatar Lokesh Singh

animal

model class and service, controller deleted
parent 348282f6
package com.lokesh.webclientconsumer.controller;
import com.lokesh.webclientconsumer.model.Animal;
import com.lokesh.webclientconsumer.service.AnimalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Slf4j
@RestController
@RequestMapping(value = "/animal")
public class AnimalController {
@Autowired
private AnimalService animalService;
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseStatus(HttpStatus.OK)
public Flux<Animal> getAnimal() {
return animalService.getAnimals();
}
@GetMapping(value = "/{id}")
public Mono<Animal> findById(@PathVariable("id") String id) {
try{
return animalService.findById(id);
} catch (Exception e) {
log.error("Exception find in findById() method.");
throw e;
}
}
@PostMapping(MediaType.APPLICATION_JSON_VALUE)
public Mono<Animal> postAnimal(@RequestBody Animal animal) {
return this.animalService.save(animal);
}
}
package com.lokesh.webclientconsumer.model;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Builder
@Document
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Animal {
@Id
private String id;
private String name;
private String kingdom;
}
package com.lokesh.webclientconsumer.runner;
import com.lokesh.webclientconsumer.model.Animal;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
@Component
public class GetAllAnimalRunner implements CommandLineRunner {
private static final String baseUrl = "http://localhost:8081/api/v1/";
@Override
public void run(String... args) throws Exception{
WebClient client = WebClient.create(baseUrl);
Flux<Animal> animalFlux = client
.get()
.uri("/animal")
.retrieve()
.bodyToFlux(Animal.class);
animalFlux.doOnNext(System.out::println).blockLast();
}
}
package com.lokesh.webclientconsumer.service;
import com.lokesh.webclientconsumer.model.Animal;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public interface AnimalService {
Flux<Animal> getAnimals();
Mono<Animal> findById(String id);
Mono<Animal> save(Animal animal);
}
package com.lokesh.webclientconsumer.service.impl;
import com.lokesh.webclientconsumer.model.Animal;
import com.lokesh.webclientconsumer.service.AnimalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static com.lokesh.webclientconsumer.constants.AnimalConstants.*;
@Slf4j
@Service
public class AnimalServiceImpl implements AnimalService {
private WebClient client = WebClient.create(BASE_URL);
public Flux<Animal> getAnimals() {
return client.get()
.uri(GET_ALL_ANIMALS)
.retrieve()
.bodyToFlux(Animal.class);
}
@Override
public Mono<Animal> findById(String id) {
try {
return client.get()
.uri(GET_ANIMAL_BYID, id)
.retrieve()
.bodyToMono(Animal.class);
} catch (WebClientResponseException wcre) {
log.error("Error response code is { } and response body is { }", wcre.getRawStatusCode(), wcre.getResponseBodyAsString());
log.error("Exception in method retrieveAllInvoices()", wcre);
throw wcre;
} catch (Exception ex) {
log.error("Exception find in findById() method.");
throw ex;
}
}
@Override
public Mono<Animal> save(Animal animal) {
return client.post()
.uri("/animal")
.retrieve()
.bodyToMono(Animal.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment