Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nas 09 #73

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
/.idea/
/.run/
/.vscode/
/config-server/target/
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
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
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;
}
}
Original file line number Diff line number Diff line change
@@ -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> T getForEntity(Class<T> eClass, String url, Object... uriVariables){
try {
ResponseEntity<String> 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 <T> List<T> getForList(Class<T> clazz, String url, Object... uriVariables) {
try {
ResponseEntity<String> 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, R> T postForEntity(Class<T> clazz, String url, R body, Object... uriVariables) {
HttpEntity<R> request = new HttpEntity<>(body);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class, uriVariables);
JavaType javaType = objectMapper.getTypeFactory().constructType(clazz);
return readValue(response, javaType);
}

// Update
public <T, R> T putForEntity(Class<T> clazz, String url, R body, Object... uriVariables) {
HttpEntity<R> request = new HttpEntity<>(body);
ResponseEntity<String> 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> T readValue(ResponseEntity<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 3 additions & 2 deletions config-server/target/classes/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ server:
port: 8888

spring:
profiles:
active: config-rep
devtools:
livereload:
enabled: true
Expand All @@ -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
Binary file not shown.