Spring RestTemplate readin JSON Document - spring-boot

I have an elementary question using Spring RestTemplate for reading a JSON document. I make a get call to a URL which returns a JSON document in this format:
{
{
..
},
{
..
}
}
I couldn't find any examples for fetching this kind of data.. Would be helpful if someone could help me out with this...

I was able to solve this problem in my case using the firebase-client apis. That exposes a map of values for the kind of json i was talking about. Still, I would like to know how to do this using RestTemplate..

Related

AWS AppSync - formatting hardcoded JSON data in response mapping template

I need a GraphQL query returning hardcoded informations as JSON response.
In the AppSync GraphQL schema, I added the following query:
type Query {
getHealthCheck: AWSJSON
}
My response mapping template where the values are hardcoded is the following:
$util.toJson({"version": "0.1.0"})
However, as the response of the query, I get a string instead of a proper JSON, i.e.:
{
"data": {
"getHealthCheck": "{\"version\":\"0.1.0\"}"
}
}
How can I modify the response mapping template to get a proper JSON? I tried several utils but I'm a bit lost with the data structures in VTL.
I don't think this is possible.
If you think about it, it goes against the idea of even having a GraphQL schema to then return essential arbritrary JSON.
I believe that AWSJSON will parse stringified JSON for inputs, but serialize to stringified JSON for outputs.
There's an answer to an other question that might give some ideas of how to work around this. Other than that, it seems your client will need to parse the JSON.

Call another rest api with payload from my server in Spring-Boot kotlin

I want to call another web-api from my server with payload using post method,
For example,
https://api.example.com.bd/subscription/query-base
Payload :
Content type
application/json;charset=utf-8
{
"applicationId": "APP_000201",
"password": "39a8d1cb245029d0560619a2b388669c"
}
Response samples :
{
"baseSize": "0",
"version": "1.0.",
"statusCode": "S1000",
"statusDetail": "Success."
}
I want to return only baseSize that method how to do it
I'm assuming you're talking about making a request to a REST API. The way to do this in Spring is to use RestTemplate. You will typically want to define a bean to inject the rest template into whatever class you want to make the call from. You will likely be using one of the following methods to call your API: exchange, postForObject, or postForEntity.
I would recommend reading some documentation on how to use the RestTemplate before going further. Here is a good starting point. The code is in Java, but the basic idea is the same in Kotlin.

Insert into elasticsearch with an id that contains slashes

Hello I am trying to insert an object into elasticsearch using it's API, the problem is that the IDs of elements that I want to insert are like this : ee5z4d5/54zd15zd/5zd45
when I sent a post request to host/index/id with a body, I got an error because the request url is host/index/ee5z4d5/54zd15zd/5zd45
I am using spring boot with feign client to comminucate with elasticsearch, and my question is how I can solve this problem
You need to URL-encode your ID first, i.e. the URL must look like this
host/index/ee5z4d5%2F54zd15zd%2F5zd45
I don't know Feign but this issue might provide some insights on how to solve your issue.
Tldr;
This is not an Elastic issue but more a web issue.
What you need to do, is encode the special char in the url.
Look at the solution below to find out what it means.
Solution
POST /73690410/_doc/ee5z4d5%2F54zd15zd%2F5zd45
{
"data": "my id has some slash"
}

Swagger return response: "can't parse JSON. Raw result:"

I am not sure why this is happening. My return statement for spring boot rest controller is something like below
return ResponseEntity.status(HttpStatus.OK).body("Successfully added");
Now on swagger UI i see response as below
can't parse JSON. Raw result:
Successfully added
why is this happening ?
This is happening because you are returning a string literal instead of a JSON object. I presume your return type would have been ResponseEntity<String> but this would not work and you would need to specify an object in the body. You can create a POJO that will hold the message for you, something like this:
public class YourResponseClass {
private String message;
//Constructors, getter and setter
}
Then while returning you need to change the return type to ResponseEntity<YourResponseClass> and then return your response:
return ResponseEntity.ok(new YourResponseClass("Successfully Added."));
Late to the game but will share my solution to this issue, as it surfaced for me in a Python Flask-based REST backend I was developing recently. You could have NaN values in your JSON, if your issue was similar to mine, albeit in Python.
My underlying issue
I too was getting "can't parse JSON" on the swagger side. Turns out that Python's standard library json import will allow "modern" but not-officially-JSON types, such as "NaN". And doing some calculations with floats and voila...As a strict parser fails with a type such as NaN, it's worth checking that you don't have these in your request JSON response.
Solved it with an encoder class
Though not a Java-specific answer, maybe my Python solution can help you solve the issue.
json.dumps(yourdata, cls=NumpyEncoder)
Where NumpyEncoder is some class that converts NaN values to JSON and/or your acceptable specifications. It might be the case that you too can include some encoder class in your code to remove NaN and/or other artifacts.
See my so answer for Python implementation details.
It is hard to say without seeing more code and the actual Swagger JSON, but it seems like the JSON has a syntax issue. Run it through a JSON validator to confirm. There are plenty of websites for this if you Google it.
The response type should be "text/plain" (produces = "MediaType.TEXT_PLAIN")

I want to consume a GraphQL web API in .NET

I want to consume data from a GraphQL API.
how can I achieve that? I am new to webapi and any help is appreciated
Assuming the API you want to consume uses HTTP, you should be able to use cUrl, wget, Charles, Postman or even just the URL bar in a browser to make a request.
To write your first query, you can start with the following:
query theNameOfMyQuery {
}
Now that you have a named query, you can start populating it with whatever fields your GraphQL server is exposing. For a blog example, you might have something like this:
query theNameOfMyQuery {
posts {
title
author
}
}
Now, to turn that into something you can request, all you need to do is URL encode it and add it to your URL. A typical URL looks like this:
https://www.someserver.com/?query=...&variables=...
So for the above example, you would have the above query
https://www.someserver.com/?query=query%20theNameOfMyQuery%20%7B%0D%0A%20%20posts%20%7B%0D%0A%20%20%20%20title%0D%0A%20%20%20%20author%0D%0A%20%20%7D%0D%0A%7D
Some Resources:
Evolution of API Design - this video explains some of the concepts of GraphQL and why it exists.
howtographql.com - This is an amazing set of tutorials for every implementation you could imagine

Resources