I am created a rest API in spring boot which consumes third party API response and return the response.
Challenges : Third party API response {"price":123456789123456789123456789.05}
I want to get the "price" from the single object ,So it will return 123456789123456789123456789.05 as expected
Expected Output : 123456789123456789123456789.05
Actual Output : 1.2345678912345679e+26
Note:
The actual output will given as exponential format but i want actual data without exponential.
Code: pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sample</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tag::actuator[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- end::actuator[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SampleApplication.java
package com.example.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
HelloController.java
package com.example.sample;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@RestController
public class HelloController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/")
public double index() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.set("token", "rtp");
HttpEntity entity = new HttpEntity < String > ("parameters", headers);
String END_Point_Test_API = "http://localhost:8080/api/test";
ResponseEntity < String > responseData = restTemplate.exchange(END_Point_Test_API, HttpMethod.GET, entity, String.class);
//Third party response {"price":123456789123456789123456789.05}
System.out.println(responseData.getBody());
String data = responseData.getBody();
JSONParser parser = new JSONParser();
JSONObject jsobj = (JSONObject) parser.parse(data);
//double price =(double)jsobj.get("price");
double price = Double.parseDouble(String.valueOf(jsobj.get("price")));
return price;
}
}