mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2022-02 - 34 - rick and morty
This commit is contained in:
@@ -7,10 +7,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.retry.annotation.EnableRetry;
|
||||
import ru.otus.spring.dto.Country;
|
||||
import ru.otus.spring.service.CountryService;
|
||||
|
||||
import java.util.List;
|
||||
import ru.otus.spring.dto.CharacterDto;
|
||||
import ru.otus.spring.service.CharacterService;
|
||||
|
||||
@EnableCaching
|
||||
@EnableRetry
|
||||
@@ -21,11 +19,11 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = SpringApplication.run(Main.class, args);
|
||||
CountryService service = ctx.getBean(CountryService.class);
|
||||
CharacterService service = ctx.getBean(CharacterService.class);
|
||||
|
||||
Country country = service.getCountry("col");
|
||||
CharacterDto characterDto = service.getCharacter(1);
|
||||
|
||||
log.info(country.getName());
|
||||
log.info(characterDto.toString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -10,6 +10,6 @@ public class CacheConfig {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
return new ConcurrentMapCacheManager( "countries" );
|
||||
return new ConcurrentMapCacheManager( "characters" );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package ru.otus.spring.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class CharacterDto {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
private String url;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CharacterDto{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", url='" + url + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package ru.otus.spring.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Country {
|
||||
|
||||
private String name;
|
||||
|
||||
private String alpha3Code;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName( String name ) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAlpha3Code() {
|
||||
return alpha3Code;
|
||||
}
|
||||
|
||||
public void setAlpha3Code( String alpha3Code ) {
|
||||
this.alpha3Code = alpha3Code;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.dto.CharacterDto;
|
||||
|
||||
public interface CharacterService {
|
||||
|
||||
CharacterDto getCharacter(int id);
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import ru.otus.spring.dto.CharacterDto;
|
||||
|
||||
@Service
|
||||
public class CharacterServiceRest implements CharacterService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CharacterServiceRest.class);
|
||||
|
||||
private RestOperations rest = new RestTemplate();
|
||||
|
||||
@Override
|
||||
public CharacterDto getCharacter(int id) {
|
||||
log.info("Request");
|
||||
return rest.getForObject("https://rickandmortyapi.com/api/character/" + id, CharacterDto.class);
|
||||
}
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.dto.Country;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CountryService {
|
||||
|
||||
Country getCountry( String id );
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestOperations;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import ru.otus.spring.dto.Country;
|
||||
|
||||
@Service
|
||||
public class CountryServiceRest implements CountryService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger( CountryServiceRest.class );
|
||||
|
||||
private RestOperations rest = new RestTemplate();
|
||||
|
||||
@Override
|
||||
public Country getCountry( String id ) {
|
||||
log.info( "Request" );
|
||||
return rest.getForObject( "http://api.countrylayer.com/v2/alpha/" + id + "?access_key=[!!!Your key!!!]", Country.class );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user