Integration with URL Shortener
d) Integration with URL Shortener - Integration with URL shortener requires the following steps -
i) Create a new class by the name of UrlShortnerUtil
ii) Annotate this class with @Component
and add the following code -
@Autowired
private RestTemplate restTemplate;
@Autowired
private VTRConfiguration config;
public String getShortenedUrl(String url){
HashMap<String,String> body = new HashMap<>();
body.put("url",url);
StringBuilder builder = new StringBuilder(config.getUrlShortnerHost());
builder.append(config.getUrlShortnerEndpoint());
String res = restTemplate.postForObject(builder.toString(), body, String.class);
if(StringUtils.isEmpty(res)){
log.error("URL_SHORTENING_ERROR", "Unable to shorten url: " + url); ;
return url;
}
else return res;
}
iii) Add the following properties in application.properties file -
#url shortner
egov.url.shortner.host=https://dev.digit.org
egov.url.shortner.endpoint=/egov-url-shortening/shortener