From abbfd7c360ba03abab7be0b3d0e8073bf8a583e1 Mon Sep 17 00:00:00 2001 From: Anas Abbal <87352192+anasabbal@users.noreply.github.com> Date: Mon, 27 Feb 2023 21:05:28 +0100 Subject: [PATCH 1/2] begin updating to release 0.0.3 => add generic RestTemplate in config server --- .../server/ConfigServerApplication.java | 12 -- .../nas/config/server/RestTemplateHelper.java | 107 ++++++++++++++++++ .../nas/config/server/config/JavaConfig.java | 18 +++ config-server/target/classes/bootstrap.yml | 5 +- .../server/ConfigServerApplication.class | Bin 1159 -> 938 bytes 5 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 config-server/src/main/java/com/nas/config/server/RestTemplateHelper.java create mode 100644 config-server/src/main/java/com/nas/config/server/config/JavaConfig.java diff --git a/config-server/src/main/java/com/nas/config/server/ConfigServerApplication.java b/config-server/src/main/java/com/nas/config/server/ConfigServerApplication.java index b521d5a..9a24cc2 100644 --- a/config-server/src/main/java/com/nas/config/server/ConfigServerApplication.java +++ b/config-server/src/main/java/com/nas/config/server/ConfigServerApplication.java @@ -1,11 +1,9 @@ package com.nas.config.server; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; -import org.springframework.web.bind.annotation.GetMapping; @SpringBootApplication @EnableConfigServer @@ -13,14 +11,4 @@ public class ConfigServerApplication{ public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } - - - @Value("${server.url:Unable to connect to config server}") - private String uri; - - - @GetMapping("/server/url") - public String get(){ - return this.uri; - } } diff --git a/config-server/src/main/java/com/nas/config/server/RestTemplateHelper.java b/config-server/src/main/java/com/nas/config/server/RestTemplateHelper.java new file mode 100644 index 0000000..27849f0 --- /dev/null +++ b/config-server/src/main/java/com/nas/config/server/RestTemplateHelper.java @@ -0,0 +1,107 @@ +package com.nas.config.server; + + +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.CollectionType; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.util.List; + +@Slf4j +@Component +public class RestTemplateHelper { + + private RestTemplate restTemplate; + private ObjectMapper objectMapper; + + + @Autowired + public RestTemplateHelper(RestTemplateBuilder restTemplateBuilder, ObjectMapper objectMapper){ + this.restTemplate = restTemplateBuilder.build(); + this.objectMapper = objectMapper; + } + + // find by id + public T getForEntity(Class eClass, String url, Object... uriVariables){ + try { + ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class, uriVariables); + JavaType javaType = objectMapper.getTypeFactory().constructType(eClass); + return readValue(responseEntity, javaType); + }catch (HttpClientErrorException exception){ + if(exception.getStatusCode() == HttpStatus.NOT_FOUND) + log.info("[+] No data found {}", url); + else + log.info("[+] Rest Client exception {}", exception.getMessage()); + } + return null; + } + + // find all + public List getForList(Class clazz, String url, Object... uriVariables) { + try { + ResponseEntity response = restTemplate.getForEntity(url, String.class, uriVariables); + CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, clazz); + return readValue(response, collectionType); + } catch (HttpClientErrorException exception) { + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { + log.info("No data found {}", url); + } else { + log.info("rest client exception", exception.getMessage()); + } + } + return null; + } + + // Save + public T postForEntity(Class clazz, String url, R body, Object... uriVariables) { + HttpEntity request = new HttpEntity<>(body); + ResponseEntity response = restTemplate.postForEntity(url, request, String.class, uriVariables); + JavaType javaType = objectMapper.getTypeFactory().constructType(clazz); + return readValue(response, javaType); + } + + // Update + public T putForEntity(Class clazz, String url, R body, Object... uriVariables) { + HttpEntity request = new HttpEntity<>(body); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.PUT, request, String.class, uriVariables); + JavaType javaType = objectMapper.getTypeFactory().constructType(clazz); + return readValue(response, javaType); + } + + // Delete + public void delete(String url, Object... uriVariables) { + try { + restTemplate.delete(url, uriVariables); + } catch (RestClientException exception) { + log.info(exception.getMessage()); + } + } + + + private T readValue(ResponseEntity responseEntity, JavaType javaType){ + T result = null; + if (responseEntity.getStatusCode() == HttpStatus.OK || + responseEntity.getStatusCode() == HttpStatus.CREATED) { + try { + result = objectMapper.readValue(responseEntity.getBody(), javaType); + } catch (IOException e) { + log.info(e.getMessage()); + } + } else { + log.info("No data found {}", responseEntity.getStatusCode()); + } + return result; + } +} diff --git a/config-server/src/main/java/com/nas/config/server/config/JavaConfig.java b/config-server/src/main/java/com/nas/config/server/config/JavaConfig.java new file mode 100644 index 0000000..2357b5b --- /dev/null +++ b/config-server/src/main/java/com/nas/config/server/config/JavaConfig.java @@ -0,0 +1,18 @@ +package com.nas.config.server.config; + + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class JavaConfig { + + + @Bean + @LoadBalanced + public RestTemplate restTemplate(){ + return new RestTemplate(); + } +} diff --git a/config-server/target/classes/bootstrap.yml b/config-server/target/classes/bootstrap.yml index bdde28b..73e48b0 100644 --- a/config-server/target/classes/bootstrap.yml +++ b/config-server/target/classes/bootstrap.yml @@ -2,6 +2,8 @@ server: port: 8888 spring: + profiles: + active: config-rep devtools: livereload: enabled: true @@ -11,5 +13,4 @@ spring: config: server: git: - uri: https://github.com/anasabbal/mini-uber-microservice/tree/master/config-server - default-label: master + uri: https://github.com/anasabbal/config-repo.git \ No newline at end of file diff --git a/config-server/target/classes/com/nas/config/server/ConfigServerApplication.class b/config-server/target/classes/com/nas/config/server/ConfigServerApplication.class index 17c2d6c7466c64f27a3eba1b4808e33b35484535..de14a838b9f3582b990ffd4acc8b84b58c932af1 100644 GIT binary patch delta 323 zcmYjLJxc>Y6r8;?UhWXoyZ8}36$>qbu-zewB1XIP)@R%ti*Z*r>lX-?*0TS>!Y-9! zVPz%$Ch=uOl;zDZJMYbV_wiGde0;sW0ocLHFo~FZXOHeP*l?RH4|Y2bw@sIY>uld` zh51@v3T63;9CY=iFgN^AmB)Q?@T=S2cSX2&$53>%srcaBNLBSrc_yy3Iptlgthlzk zR7%^@O09TXtDdYbj91%OMTCIv0U<*TI58uxwVuI--ku^H*}ybCVB)6%DP%@6 zMgueSHJUDb8c^B$X6AWkucMdF-liE0`626Om2 A+5i9m delta 508 zcmZWlO)mpc6g_t)Rc(E>wA4q9ueKyS3oAupWrK~dVD(y0GctK2(`iLSEN#jB0}Bf) z5;hiofPdkSh&xlFiN(A3-1pAC=iK)gzeyxNzTVyetfTy#jMH2KaSZezjSRtTdNv`m zeJWd0I8xWep6}VZUI{*_Kr6wfkv*&`sT<-%)_m8y5K?Q`m%i<4aUh+hst{tW5D=DU zFB{5hDQ~IiIjj3xRvl&eu2pljRyF?zp4fG(O}h%x^iVMC$|nq!=l`1^L=G0uRaI1N zecUO&rTiUvb{2Xiq(pn7VBz4EmS`h%es>!SMvWq^aWNj;8 Date: Mon, 27 Feb 2023 21:15:56 +0100 Subject: [PATCH 2/2] begin updating to release 0.0.3 => add generic RestTemplate in config server --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 383593c..54e0fd7 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ /.idea/ /.run/ /.vscode/ +/config-server/target/