I done Spring rest api .that is returning json data now i want to call that api in my system(remote by ip) .how to get that response in my java code
Or Rest Template.
{[
{
"deviceId": 1,
"userId": "100",
"userName": "Jee",
"date": "2016-09-19 00:00:00.000"
},
.
.
.
n
]}
how to read in java using rest Template .
What you want is consuming a rest api in Java. This should get you started.
Its a pretty much a basic need and its all over the web on how to consume rest apis.
This and this are good starting points.
Quoting the relevant parts here:
Use POST to Create a Resource
In order to create a new Resource in the API – we can make good use of the postForLocation(), postForObject() or postForEntity() APIs.
The first returns the URI of the newly created Resource while the second returns the Resource itself.
4.1. The postForObject API
ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class);
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));
4.2. The postForLocation API
Similarly, let’s have a look at the operation that – instead of returning the full Resource, just returns the Location of that newly created Resource:
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
URI location = restTemplate.postForLocation(fooResourceUrl, request);
assertThat(location, notNullValue());
4.3. The exchange API
Finally, let’s have a look at how to do a POST with the more generic exchange API:
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
ResponseEntity<Foo> response = restTemplate.
exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
Foo foo = response.getBody();
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));
You'd do good with the post exchange API.
Update
Your json response is not valid. You need to change the rest api to return something like this
{
"results": [
{
"deviceId": 1,
"userId": "100",
"userName": "Jee",
"date": "2016-09-19 00:00:00.000"
},
{
"deviceId": 1,
"userId": "100",
"userName": "Jee",
"date": "2016-09-19 00:00:00.000"
}
]
}
When consuming this rest api, the response object will be
public class ResponseObject {
private List<BiomatrixResult> results;
//getter setters
}
where your BiomatrixResult object will be
public class BiomatrixResult {
private int deviceId;
private String userId;
private String userName;
private Date date;
//getters setters
}
Related
I am working on a Spring boot project, it produces strange behaviors, for ex:
I have two APIs as follow
Controller file
#GetMapping("/list/employees")
public ResponseEntity<List<Employee>> getEmployees(){
List<Employee> list = employeeService.getAllEmployees();
return new ResponseEntity<List<Employee>>(list, new HttpHeaders(), HttpStatus.OK );
}
#GetMapping("employee/{id}")
public ResponseEntity<Employee> getEmployeeById(#PathVariable("id") long id) throws RuntimeException{
Employee employee = employeeService.getEmployee(id);
return new ResponseEntity<Employee>(employee,new HttpHeaders(),HttpStatus.OK);
}
Service file
/* return all employees */
public List<Employee> getAllEmployees(){
List<Employee> listEmployee = employeeRepo.findAll();
if(listEmployee.size()>0){
return listEmployee;
}else{
return new ArrayList<Employee>();
}
}
/*
RETURN SINGLE EMPLOYEE BY ID
*/
public Employee getEmployee(long id) throws RuntimeException{
Optional<Employee> employee = employeeRepo.findById(id);
if(employee.isPresent()){
return employee.get();
}else{
new RuntimeException("Record not found");
}
return null;
}
But running them in Postman gives weird output, for ex:
Correct behavior of second API returning single employee
http://127.0.0.1:8080/employee/3
{
"id": 3,
"firstName": "Caption",
"lastName": "America",
"email": "cap#marvel.com"
}
Incorrect behavior of the same API (I am typing the wrong path this time)
http://127.0.0.1:8080/employees/3
The API path is wrong (employees/3)
{
"firstName": "Caption",
"lastName": "America",
"email": "cap#marvel.com",
"_links": {
"self": {
"href": "http://127.0.0.1:8080/employees/3"
},
"employee": {
"href": "http://127.0.0.1:8080/employees/3"
}
}
}
same behavior with the root URI, I have not triggered any action with home URI but still gives output like in the above API.
what can be the reason for these unwanted API calls?
Looks like you have Spring Data Rest on your class path. It will automatically wire paths based on the repositories. That second response is a HATEOAS response.
A simple test would be to check maven/gradle. If you see spring-data-rest, comment it out and try again.
There is no unwanted API calls. That is the way HATEOS response is represented as stated in the documentation:
The fundamental idea of hypermedia is to enrich the representation of a resource with hypermedia elements. The simplest form of that are links. They indicate a client that it can navigate to a certain resource. The semantics of a related resource are defined in a so-called link relation.
As suggested above, try to look for spring boot hateos dependency and comment or remove that, then it should revert back to normal REST JSON response.
If you are using maven, look for :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
if you are using gradle, look for :
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
I am getting below response when I am calling an API.
Response postRequestResponse = ConnectionUtil.getwebTarget()
.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true)
.path("bots")
.path(ReadSkillID.readSkillId())
.path("dynamicEntities").path(dynamicEntityID)
.path("pushRequests").path(pushRequestID).path(operation)
.request()
.header("Authorization", "Bearer " + ConnectionUtil.getToken())
.get();
Below output I am getting.
{
"createdOn": "2020-08-17T12:19:13.541Z",
"updatedOn": "2020-08-17T12:19:23.421Z",
"id": "C84B058A-C8F9-41F5-A353-EC2CFE7A1BD9",
"status": "TRAINING",
"statusMessage": "Request Pushed into training, on user request"
}
I have to return this output to client with an additional field in the response. How can modify the above response and make it
{
"EntityName": "NewEntity", //New field
"createdOn": "2020-08-17T12:19:13.541Z",
"updatedOn": "2020-08-17T12:19:23.421Z",
"id": "C84B058A-C8F9-41F5-A353-EC2CFE7A1BD9",
"status": "TRAINING",
"statusMessage": "Request Pushed into training, on user request"
}
I am adding this additional field here
"EntityName": "NewEntity"
How can I do that. many things I tried but got exception.
get JSON from postRequestResponse (i have no idea what framework you are using, so you have to figer it out on your own, but the Response datatype will probably have a getResponseBody or similar method returing the JSON)
add EntityName
serialize it again to json.
class YourBean {
#Autowired
private ObjectMapper objectMapper;
public void yourMethod() {
// 1
final InputStream jsonFromResponse = ...
// 2
Map dataFromResponse = objectMapper.readValue(jsonFromResponse, Map.class);
dataFromResponse.put("EntityName", "NewEntity");
// 3
final String enrichedJson = objectMapper.writeValueAsString(dataFromResponse);
}
}
enrichedJson contains EntityName and whatever comes from the API.
I am currently working on a project where i need to make a rest call to an external API and parse the JSON response to a POJO and return back the POJO as JSON for another rest request. I am able to parse the JSON response, but my requirement is to parse only one particular node from it. How can i achieve this? I am using Spring Boot and Spring Rest Template to make the external rest call. Please help!!!
#RestController
public class ProductsController {
private static final Logger LOGGER = LoggerFactory.getLogger(ProductsController.class);
#RequestMapping(value = "/myRetail/product/{id}", method = RequestMethod.GET, produces = {
MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.APPLICATION_XML_VALUE })
#ResponseBody
public Item getSchedule(#Valid Payload payload) {
String URL = "<External API>";
LOGGER.info("payload:{}", payload);
Item response = new Item();
RestTemplate restTemplate = new RestTemplate();
Item item = restTemplate.getForObject(URL, Item.class);
LOGGER.info("Response:{}", item.toString());
return response;
}
}
JSONResponse (This is a part of whole i receive)
{
"ParentNode": {
"childNode": {
"att": "13860428",
"subchildNode 1": {
"att1": false,
"att2": false,
"att3": true,
"att4": false
},
"att4": "058-34-0436",
"att5": "025192110306",
"subchildenode2": {
"att6": "hello",
"att7": ["how are you", "fine", "notbad"],
"is_required": "yes"
},
............
}
Required JSONpart from the above whole response:
"subchildenode2": {
"att6": "hello",
"att7": ["how are you", "fine", "notbad"],
"is_required": "yes"
}
Use the org.json library. With this library you can parse the payload to a JSONObject and navigate to your required subpart of the document.
So you have to get the payload as a JSON-String and parse it to the JSONObject from the library. After that you can navigate to your required subpart of the document and extract the value and then parse it to your required Java POJO.
Have look at: How to parse JSON
Just map the path to the needed object:
{
"ParentNode": {
"childNode": {
"subchildenode2": {
"att6": "hello",
"att7": ["how are you", "fine", "notbad"],
"is_required": "yes"
}
}
}
And then simply:
Response responseObject= new Gson().fromJson(json, Response.class);
SubChildNode2 att6 = responseObject.getParentNode().getChildNode().getSubChildNode2();
let's say I've got a REST API which I could get list of books by invoking following retrofit 2 request.
public interface AllRecordsFromRequestInterface {
#GET("books/all")
Call<List<TrackInfo>> operation(#Header("Authorization") String authentication_token);
}
and API response:
[
{
"id": "1",
"title": "The Catcher in the Rye",
"author":"J. D. Salinger"
},
{
"id": "2",
"title": "The Great Gatsby",
"author":"F. Scott Fitzgerald"
}
]
I use GsonConverterFactory to convert json to a Model. here is my model class
public class Book{
private int id;
private String title;
private String author;
}
I'm using a authentication token to authorize myself to API as it can be seen in my request. some times other response are received rather than above response because of token expiration or something else. for example:
{
"status": "error",
"message": "Expired token"
}
what is the proper way to handle dynamic responses (with known structure) in retrofit 2?
you have multiple choices:
1-change your API:(this one is standard)
change it like this for every response and if the user failed with authentication leave the result null or if authentication was successful put the list in the result.
{
"status" : "error/success"
"message" : ...
"result" : ....
}
2- you can give Object type to retrofit and after the response was successful you can cast it to one of your models, using "instance of" syntax.
public interface AllRecordsFromRequestInterface {
#GET("books/all")
Call<Object> operation(#Header("Authorization") String authentication_token);
}
I am using apache cxf webclient to consume a service written in .NET
sample JSON to be sent in request body to a web service
{
"Conditions":
[
{
"Field":"TextBody",
"Comparer":"ContainsAny",
"Values":["stocks","retire"],
"Proximity":0
},
{
"Field":"SentAt",
"Comparer":"LessThan",
"Values":["1331769600"],
"Proximity":0
},
],
"Operator":"And",
"ExpireResultIn":3600
}
Is there any way if I want to submit data from both form and in Json body in one request ?
webclient API apache CXF -
web client API doc
WebClient client = WebClient.create("http://mylocalhost.com:8989/CXFTest/cxfws/rest/restservice/json");
client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
After this which method and how to use ?
client.form(...form object )
client.post(...JSON string )
They have not shared Object of "Conditions" in JSON which I can annotate and pass to post method of client
I got answer here
Need to set JSON provider in my case it was jackson
List<Object> providers = new ArrayList<Object>();
providers.add( new JacksonJaxbJsonProvider() );
WebClient client = WebClient.create("http://localhost:8080/poc_restapi_cxf/api",
providers);
client = client.accept("application/json")
.type("application/json")
.path("/order")
.query("id", "1");
Order order = client.get(Order.class);
System.out.println("Order:" + order.getCustomerName());
There is a way to do this using annotations and suited my purpose:
#Post
#Path("mypath/json/whatever")
#Consumes({MediaType.APPLICATION_JSON_VALUE})
public Response postClient(#Context HttpHeaders headers, String input) {
//Here the String input will be equal to the supplied json.
//...
}