How do i create a POST request using jax -rs - java

I have two classes PlaylistResource which has a method createPlaylist which takes an object of type PlaylistRequest. I want to create a POST request on localhost:9999/playlists I am using Postman and I am not sure how to pass the object of PlaylistRequest which is request to the method createPlaylist.
#XmlType(propOrder= {"title", "artistSeeds", "numberOfSongs"})
#XmlAccessorType(XmlAccessType.FIELD)
public class PlaylistRequest {
private String title = "";
#XmlElement(name = "seeds")
private List<String> artistSeeds;
#XmlElement (name = "size")
private int numberOfSongs = 0;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getArtistSeeds() {
return artistSeeds;
}
public void setArtistSeeds(List<String> artistSeeds) {
this.artistSeeds = artistSeeds;
}
public int getNumberOfSongs() {
return numberOfSongs;
}
public void setNumberOfSongs(int numberOfSongs) {
this.numberOfSongs = numberOfSongs;
}
}
The other class:
#Path("playlists")
public class PlaylistResource implements PlaylistApi {
#Override
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response createPlaylist(PlaylistRequest request) {
if(request == null) {
System.out.println("Was here");
throw new ClientRequestException(new ErrorMessage("no parameter passed."));
}
try {
List<Song> playList = new ArrayList<>();
List<Song> songs = new ArrayList<>();
List<String> artistsIds = new ArrayList<>();
ArtistResource artistsResources = new ArtistResource();
int playlistDefaultSize = 10;
int i = 0;
do {
playList.add(songs.get(i));
i++;
}while( i < playlistDefaultSize);
Playlist playlist = new Playlist();
playlist.setTitle(request.getTitle());
playlist.setSize(songs.size());
playlist.setTracks(playList);
return Response.status(Response.Status.CREATED.getStatusCode()).entity(playlist).build();
} catch (Exception e) {
throw new RemoteApiException(new ErrorMessage(e.getMessage()));
}
}
}

Change this parameter from data class to string,
public Response createPlaylist(PlaylistRequest request) {
to
public Response createPlaylist(String request) {
then convert it using GSON into your data class.
PlaylistRequest request = new Gson().fromJson(request, new TypeToken<PlaylistRequest >(){}.getType());

Simply add a JSON object in Body of request, select Raw Json in Postman and paste the following object:
NOTE: Add a key Content-Type and set its value to application/json in Header of Request
{
"title": "Some title",
"artistSeeds": [
"string1",
"string2"
],
"numberOfSongs": 0
}
HEADER
BODY

Related

Get the value of items of a Json with Java Spring Boot

I'm trying to extract API data from the Json file below.
I want to retrieve the "name" of each "item".
Once the "name" is retrieved, I want to create a new Json that will contain :
{name: "toto", name: "titi"....}
The goal is then to create an API on my side which on a call from http://localhost/getitems will return the result of the Json created.
I'm new to Java and Spring Boot, so if you think there is a code that is easier, let me know, i hope you can help me to create that new Json file easily. Thanks !
// Json File (it has been reduced, more than 700 name are present)
{
"kind": "Space",
"apiVersion": "v1",
"metadata": {
"selfLink": "something",
"resourceVersion": "something"
},
"items": [
{
"metadata": {
"name": "projet1"
}
},
{
"metadata": {
"name": "com-cicd"
}
}
]
}
// TestGet.java Class
public static NameSpaceJson getPostWithCustomHeaders(String DebutUrl, String MilieuUrl, String ParamUrl) {
String url = DebutUrl.concat(MilieuUrl).concat(ParamUrl);
String Bearer = "...";
// create headers & template
HttpHeaders headers = new HttpHeaders();
RestTemplate restTemplate = new RestTemplate();
// set `accept` header for the type of response
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// set custom header, bearer here too
headers.set("x-request-source", "desktop");
headers.set("Authorization", "Bearer "+Bearer);
// build the request
#SuppressWarnings({ "rawtypes", "unchecked" })
HttpEntity request = new HttpEntity(headers);
// use `exchange` method for HTTP call, this one permits us to use bearer for auth
ResponseEntity<NameSpaceJson> response = restTemplate.exchange(url, HttpMethod.GET, request, NameSpaceJson.class, 1);
if(response.getStatusCode() == HttpStatus.OK) {
return response.getBody();
} else {
return null;
}
}
// The name in the same file
public static void main(String[] args) {
TestGet.disableSSLCertificateChecking();
NameSpaceJson resultresponse = getPostWithCustomHeaders("https...","api","names");
// Long response = resultresponse.getValue().getId();
List<Item> response = resultresponse.getItems();
String test = GenerateNewJsonNameSpace.createJsonContent(response);
System.out.println(test);
}
//NameSpaceJson.java File
package com.example.consumingrest;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
#JsonIgnoreProperties(ignoreUnknown = true)
public class NameSpaceJson {
private String kind;
private String apiVersion;
private List<Item> items;
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getApiVersion() {
return apiVersion;
}
public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
//Metadata.java
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
#JsonIgnoreProperties(ignoreUnknown = true)
public class Metadata {
private String name;
private String creationTimestamp;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreationTimestamp() {
return creationTimestamp;
}
public void setCreationTimestamp(String creationTimestamp) {
this.creationTimestamp = creationTimestamp;
}
}
//Item.java
package com.example.consumingrest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
#JsonIgnoreProperties(ignoreUnknown = true)
public class Item {
Metadata metadata;
public Metadata getMetadata() {
return metadata;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
}
// GenerateNewJsonNameSpace ( this is what i have tried.. but i'm sure we can do really better.. )
package com.example.consumingrest;
import java.util.List;
public class GenerateNewJsonNameSpace {
public static String createJsonContent(List<Item> ListOfNameSpace) {
if(ListOfNameSpace.isEmpty()) {
return null;
}else {
String LeJson;
LeJson = "{";
for(int i = 0; i < ListOfNameSpace.size(); i++) {
LeJson.concat(ListOfNameSpace.get(i).getMetadata().getName());
LeJson.concat(", \n");
}
LeJson.concat("}");
return LeJson;
}
}
}
you can use a library named Gson, which is created by google specifically for handling the JSON data.
All you need to do is create a new Gson object and parse the JSON with it.
You can do in just couple of lines
String jsonString = "{ \"kind\": \"Space\", \"apiVersion\": \"v1\", \"metadata\": { \"selfLink\": \"something\", \"resourceVersion\": \"something\" }, \"items\": [ { \"metadata\": { \"name\": \"projet1\" } }, { \"metadata\": { \"name\": \"affeccom-cicd\" } } ] }";
JsonObject data = new Gson().fromJson(jsonString, JsonObject.class);
JsonArray names = data .get("items").getAsJsonArray();
for(JsonElement element : names){
JsonObject object = element.getAsJsonObject();
System.out.println(object.get("metadata").getAsJsonObject().get("name").getAsString());
}

How to do following operation in Dao implementation?

I have got an array response from the following code.
I am able to return the array result as above but how do i return a json object using one of the values from that array? I am very much new to Java, springboot and hibernate. Any help would be appretiated!
GoalPlanController
#RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {
try {
logger.info("get recurring amount by accountid:->", accountID);
AccountsDTO[] goalPlan = goalPlanService.getCalculateRecurringAmount(accountID);
return new ResponseEntity<>(goalPlan, HttpStatus.OK);
}catch(Exception ex) {
logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
ErrorDTO errors = new ErrorDTO();
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);
}
}
This is GoalPlanDaoImplementation
#Autowired
private GoalPlanRepository goalPlanRepository;
#Override
public List<Accounts> getCalculateRecurringAmount(String accountID) {
// TODO Auto-generated method stub
List<Accounts> goalPlan = null;
goalPlan = goalPlanRepository.getCalculateRecurringAmount(accountID);
return goalPlan.subList(0, 1);
}
GoalPlanRepository ->
public interface GoalPlanRepository extends JpaRepository<GoalPlan, String>{
#Query("select ac from Accounts ac where ac.userId = :accountID")
public List<Accounts> getCalculateRecurringAmount(#Param("accountID") String accountID);
}
I get a array result as follows
{
"accountID": "acc12345",
"accountName": "hellooee",
"accountType": "goalPlanner",
"userId": "abcd",
"bankName": null,
"bankId": null,
"debiitCardNumber": null,
"availableBalance": null,
}
]```
Now using accountID I need to return a json object like this
{
"calculatedGoalAmount": [
{
"goalFrequency": "Monthly",
"goalAmount": 0.4166666666666667,
"fromAccount": "acc12345"
},
{
"goalFrequency": "Quarterly",
"goalAmount": 1.25,
"fromAccount": "acc12345"
}
]
}
My AccountsDTO has folllowing
public class AccountsDTO {
private String accountID;
private String accountName;
private String accountType;
private String userId;
private String bankName;
private String bankId;
private String debitCardNumber;
//getters and setters
}
And initilAmount, goalTimePeriod, goalAmount are the values entered by user.
then i need to calculate
monthly = (goalAmount-initialAmount)/(12*goalTimePeriod)
quarterly = (goalAmount-initialAmount)/(4*goalTimePeriod)
accountId = (got from the response array above)
First You need to create two classes.
CustomResponse Class
public class CustomResponse {
private List<CalculatedGoalAmount> calculatedGoalAmount;
//getters and setters
}
CalculatedGoalAmount Class
public class CalculatedGoalAmount {
private String goalFrequency;
private double goalAmount;
private String fromAccount;
//getters and setters
}
Then inside your getCalculateRecurringAmount method write below code. Note that I have no idea about your AccountsDTO class.
#RequestMapping(method = RequestMethod.GET, path="/calculateRecurringAmount")
public ResponseEntity<Object> getCalculateRecurringAmount( String accountID) {
CalculatedGoalAmount calculatedGoalAmount = null;
CustomResponse customResponse = null;
try {
customResponse = new CustomResponse();
AccountsDTO[] goalPlan = goalPlanService.getCalculateRecurringAmount(accountID);
for (AccountsDTO accountsDTO : goalPlan) {
calculatedGoalAmount = new CalculatedGoalAmount();
calculatedGoalAmount.setFromAccount(accountsDTO.getFromAccount());
calculatedGoalAmount.setGoalAmount(accountsDTO.getGoalAmount());
calculatedGoalAmount.setGoalFrequency(accountsDTO.getFrequency());
customResponse.getCalculatedGoalAmount().add(calculatedGoalAmount);
}
return new ResponseEntity<>(customResponse, HttpStatus.OK);
}catch(Exception ex) {
logger.error("Exception raised retriving recurring amount using accountId:->" + ex);
ErrorDTO errors = new ErrorDTO();
errors.setError(ex.getMessage());
errors.setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);
}
}

Mapping JSON to Class not working

I have a class which contains other some properties of another classes and when I try to convert from json to my class, there is an error displayed.
This is my class:
import org.jongo.marshall.jackson.oid.MongoObjectId;
import org.json.JSONObject;
import java.util.List;
public class BusinessTravelDTO {
#MongoObjectId
private String id;
private String travelerId;
private BusinessTravelStatus status;
List<FlightDTO> flights;
List<HotelDTO> hotels;
List<CarDTO> cars;
public BusinessTravelDTO() { }
public BusinessTravelDTO(JSONObject data) {
this.travelerId = data.getString("travelerId");
this.status = BusinessTravelStatus.valueOf(data.getString("status"));
this.flights = HandlerUtil.getInputFlights(data.getJSONArray("flights"));
this.hotels = HandlerUtil.getInputHotels(data.getJSONArray("hotels"));
this.cars = HandlerUtil.getInputCars(data.getJSONArray("cars"));
}
public JSONObject toJson() {
return new JSONObject()
.put("id", this.id)
.put("travelerId", this.travelerId)
.put("status", this.status)
.put("flights", this.flights)
.put("hotels", this.hotels)
.put("cars", this.cars);
}
And here is where I try to convert to class:
public static JSONObject acceptBusinessTravel(JSONObject input) {
String btId = getStringField(input, "id");
MongoCollection businessTravels = getBTCollection();
// Here is the problem...
BusinessTravelDTO bt = businessTravels.findOne(new ObjectId(btId)).as(BusinessTravelDTO.class);
bt.setStatus(BusinessTravelStatus.Accepted);
businessTravels.save(bt);
return new JSONObject().put("message", "The business travel has been ACCEPTED by your manager. Check your email.");
}
Here is the error I receive:
"error": "org.jongo.marshall.MarshallingException: Unable to unmarshall result to class path.data.BusinessTravelDTO from content { \"_id\" : { \"$oid\" : \"59d6905411d58632fd5bd8a5\"} , \"travelerId\"
In jongo docs is specified that the class should have an empty constructor... http://jongo.org/#mapping I have 2 constructors, I have tried also with #JsonCreator, but no success... :(
Do you have an idea why it doesn't convert? Could it be something related to fields inside BusinesTravelDTO like List CarDTO for ex ?
I finnally found the solution;
There is needed an empty constructor in all classes FlightDTO, HotelDTO, CarDTO plus I should rewrite the toJson method as following:
public JSONObject toJson() {
JSONObject obj = new JSONObject().put("id", this.id).put("travelerId", this.travelerId).put("status", this.status);
if (flights != null) {
JSONArray flightArray = new JSONArray();
for (int i = 0; i < flights.size(); ++i) {
flightArray.put(flights.get(i).toJson());
}
obj.put("flights", flightArray);
}
if (hotels != null) {
JSONArray hotelArray = new JSONArray();
for (int i = 0; i < hotels.size(); ++i) {
hotelArray.put(hotels.get(i).toJson());
}
obj.put("hotels", hotelArray);
}
if (cars != null) {
JSONArray carArray = new JSONArray();
for (int i = 0; i < cars.size(); ++i) {
carArray.put(cars.get(i).toJson());
}
obj.put("cars", carArray);
}
return obj;
}
And this is the FlightDTO;
public class FlightDTO {
#MongoObjectId
private String id;
private String departure;
private String arrival;
private String airline;
private Double price;
public FlightDTO() {
}
public FlightDTO(JSONObject data) {
this.departure = data.getString("departure");
this.arrival = data.getString("arrival");
this.airline = data.getString("airline");
this.price = data.getDouble("price");
}
public JSONObject toJson() {
return new JSONObject()
.put("id", this.id)
.put("departure", this.departure)
.put("arrival", this.arrival)
.put("airline", this.airline)
.put("price", this.price);
}
}
now it works well! :)

How to serialize java objects as field.path = field.value

I have the following model classes:
package com.ab.model;
import java.util.List;
public class Request {
public Request(String requestType, Body body, List<String> emails) {
this.requestType = requestType;
this.body =body;
this.emails = emails;
}
private String requestType;
private Body body;
private List<String> emails;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
}
class Body {
private String content;
private List<Header> headers;
public Body(String content, List<Header> headers) {
this.content = content;
this.headers = headers;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<Header> getHeaders() {
return headers;
}
public void setHeaders(List<Header> headers) {
this.headers = headers;
}
}
class Header {
private String headerName;
public Header (String headerName) {
this.headerName = headerName;
}
public String getHeaderName() {
return headerName;
}
public void setHeaderName(String headerName) {
this.headerName = headerName;
}
}
And the following instance of the Request class:
Request request = new Request(
"get",
new Body("abcdefg",
Arrays.asList(new Header("header_one"))),
Arrays.asList("a#a.com", "b#b.com"));
Do you know any library or algorithm that can serialize the request object into the following string?
requestType = "get"
body.content = "abcdefg"
body.headers[0].headerName = "header_one"
emails[0] = "a#a.com"
emails[1] = "b#b.com"
I know I can serialize it as json, xml, etc, but these don't fit my use case.
Basically I need a serialization like:
field.nestedField.reallyNestedField = "its primitive value"
As a next step, I am planning to read the generated string and generate arbitrary data for each field/nestedField then deserialize it back using PropertyUtils from Apache e.g.:
PropertyUtils.setProperty(requestObject, "requestType", "random type");
PropertyUtils.setProperty(requestObject, "body.content", "random content");
//...
Many thanks!
Andrei
What about overriding your toString() default methods to read and output your member variables as text. You can use super to refer to your SuperClass and it's members.
PS: You don't have default constructors in your classes! In case you have constructors with your argument list it is suggested to include your no-argument default constructor in your class! Especially in case you are implementing some logic related to serialisation / deserialisation!
You can iterate and recurse on the class/object properties using Commons PropertyUtils.
Depending on how complex your implementation is, you might need to do some type checking for primitive/wrapper/collection types (the below leverages Commons ClassUtils).
public static List<String> getPropertyDescriptorPaths(Class<?> clazz) {
return getPropertyDescriptorPaths("", clazz);
}
private static List<String> getPropertyDescriptorPaths(String prefix, Class<?> clazz) {
List<String> paths = new ArrayList<>();
PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(clazz);
for (PropertyDescriptor pd : descriptors) {
if (isSimpleType(pd.getPropertyType())) {
paths.add(prefix + pd.getName());
} else if (!pd.getName().equals("class")) {
paths.addAll(getPropertyDescriptorPaths(pd.getName() + ".", pd.getPropertyType()));
}
}
return paths;
}
private static boolean isSimpleType(Class<?> clazz) {
return ClassUtils.isPrimitiveOrWrapper(clazz) || clazz.equals(String.class) || isCollectionOrArray(clazz);
}
private static boolean isCollectionOrArray(Class<?> clazz) {
return isCollection(clazz) || clazz.isArray();
}
private static final List<Class<?>> COLLECTION_TYPES = Arrays.asList(new Class<?>[] { List.class, Map.class, Set.class });
private static boolean isCollection(Class<?> clazz) {
for (Class<?> eachClass : COLLECTION_TYPES) {
if (eachClass.isAssignableFrom(clazz)) {
return true;
}
}
return false;
}
The condition for comparing property name to class is because each object has a getClass() method, and we don't care about that.
Using this with your classes, we get the result:
System.out.println(getPropertyDescriptorPaths(Request.class));
// [emails, requestType, body.headers, body.content]

Json API Parsing troubles with Java

I'm running into a few issues similar to what others have had in the past with Json parsing in Java. This is the first time I try something like this so any help/tips is extremely useful.
I'm trying to parse in data from this site: https://api.bitcoinaverage.com/exchanges/USD
I have tried numerous ways with both Json and Gson. And have tried looking for help here but to no avail.
Here are the classes that are set up (these were auto generated):
Info.java:
public class Info{
private String display_URL;
private String display_name;
private Rates[] rates;
private String source;
private Number volume_btc;
private Number volume_percent;
public String getDisplay_URL(){
return this.display_URL;
}
public void setDisplay_URL(String display_URL){
this.display_URL = display_URL;
}
public String getDisplay_name(){
return this.display_name;
}
public void setDisplay_name(String display_name){
this.display_name = display_name;
}
public Rates[] getRates(){
return this.rates;
}
public void setRates(Rates[] rates){
this.rates = rates;
}
public String getSource(){
return this.source;
}
public void setSource(String source){
this.source = source;
}
public Number getVolume_btc(){
return this.volume_btc;
}
public void setVolume_btc(Number volume_btc){
this.volume_btc = volume_btc;
}
public Number getVolume_percent(){
return this.volume_percent;
}
public void setVolume_percent(Number volume_percent){
this.volume_percent = volume_percent;
}
}
Rates.java:
public class Rates {
private Number ask;
private Number bid;
private Number last;
public Number getAsk(){
return this.ask;
}
public void setAsk(Number ask){
this.ask = ask;
}
public Number getBid(){
return this.bid;
}
public void setBid(Number bid){
this.bid = bid;
}
public Number getLast(){
return this.last;
}
public void setLast(Number last){
this.last = last;
}
}
MainClass.java:
public class MainClass {
public static void main(String[] args) throws Exception {
Gson gson = new Gson();
String json = readUrl("https://api.bitcoinaverage.com/exchanges/USD");
Info page = gson.fromJson(json, Info.class);
System.out.println(page.getDisplay_name());
}
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
}
When I try to call a getter, a null is returned.
How do I go about parsing the data properly, and then being able to call an attribute from which ever object I want? For example, if I want an attribute from "anx_hk" or "bitfinex".
This is the first time me posting something here so I hope I'm following the proper guidelines.
I also plan on passing this over to Android once I get the fell for parsing Json better. Thanks for the help! It'll greatly be appreciated.
I'll be honest with you, that's a pretty lame API response. Here it is
{
"anx_hk": {
"display_URL": "https://anxbtc.com/",
"display_name": "ANXBTC",
"rates": {
"ask": 454.26,
"bid": 444.46,
"last": 443.78
},
"source": "bitcoincharts",
"volume_btc": 11.73,
"volume_percent": 0.02
},
...,
"timestamp": "Fri, 04 Apr 2014 04:30:26 -0000",
...
}
There's no JSON array here, so you can get rid of all your array types. This response is a JSON object, which contains a bunch of JSON objects (which share a format) and a JSON name value pair where the name is timestamp.
The common JSON objects have two fields of type double (that's what type your field should be, not Number)
"volume_btc": 11.73,
"volume_percent": 0.02
, three fields of type String
"display_URL": "https://anxbtc.com/",
"display_name": "ANXBTC",
"source": "bitcoincharts",
and one that is a JSON object that contains three more doubles
"rates": {
"ask": 454.26,
"bid": 444.46,
"last": 443.78
}
The actual issue here is that, I'm assuming, the JSON objects in the root JSON object have names that may change or new ones may be added. This is not a good fit for a POJO. Instead you'd want to use a Map<String, Info>, but Gson can't map to that by default. It is not well suited for such deserialization. You'd have to provide your own TypeAdapter.
Instead, I'm going to suggest you use Jackson.
If we put that all together, we get something like
class ApiResponse {
private Map<String, Info> page = new HashMap<>();
private Date timestamp;
public Map<String, Info> getPage() {
return page;
}
#JsonAnySetter
public void setPage(String name, Info value) {
page.put(name, value);
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
class Info {
private String display_URL;
private String display_name;
private Rates rates;
private String source;
private Double volume_btc;
private Double volume_percent;
public String getDisplay_URL() {
return this.display_URL;
}
public void setDisplay_URL(String display_URL) {
this.display_URL = display_URL;
}
public String getDisplay_name() {
return this.display_name;
}
public void setDisplay_name(String display_name) {
this.display_name = display_name;
}
public Rates getRates() {
return this.rates;
}
public void setRates(Rates rates) {
this.rates = rates;
}
public String getSource() {
return this.source;
}
public void setSource(String source) {
this.source = source;
}
public Double getVolume_btc() {
return this.volume_btc;
}
public void setVolume_btc(Double volume_btc) {
this.volume_btc = volume_btc;
}
public Double getVolume_percent() {
return this.volume_percent;
}
public void setVolume_percent(Double volume_percent) {
this.volume_percent = volume_percent;
}
}
class Rates {
private Double ask;
private Double bid;
private Double last;
public Number getAsk() {
return this.ask;
}
public void setAsk(Double ask) {
this.ask = ask;
}
public Double getBid() {
return this.bid;
}
public void setBid(Double bid) {
this.bid = bid;
}
public Double getLast() {
return this.last;
}
public void setLast(Double last) {
this.last = last;
}
}
With deserialization code such as
String json = readUrl("https://api.bitcoinaverage.com/exchanges/USD");
ObjectMapper mapper = new ObjectMapper();
ApiResponse response = mapper.readValue(json, ApiResponse.class);
System.out.println(response);
With appropriate toString() methods (mine were auto-generated with Eclipse), you would get something like
ApiResponse [pages={bitkonan=Info [display_URL=https://bitkonan.com/, display_name=BitKonan, rates=Rates [ask=475.0, bid=438.01, last=437.0], source=api, volume_btc=7.24, volume_percent=0.01], vaultofsatoshi=Info [display_URL=https://vaultofsatoshi.com, display_name=Vault of Satoshi, rates=Rates [ask=460.0, bid=460.0, last=460.0], source=api, volume_btc=11.46, volume_percent=0.02], bitstamp=Info [display_URL=https://bitstamp.net/, display_name=Bitstamp, rates=Rates [ask=439.16, bid=436.34, last=436.34], source=api, volume_btc=22186.29, volume_percent=35.19], ...}, timestamp=Fri Apr 04 01:02:43 EDT 2014]
as output.
The api response contains many objects, but seems that you are trying to read them as a single Info object.
You may try to read the response as a Map<String, Info>, and iterate the entries.
Map<String, Info> hashMap = gson.fromJson(body, HashMap.class);
for (Map.Entry entry : hashMap.entrySet()) {
// your code
}

Categories