Am trying to hide few fields of an object for specific API Endpoints.
ex:
"student": {
"name": "value1",
"age": "value2",
"dob": "value3",
"value": "value4"
}
I need these some values to be present and hidden for certain API Endpoints.
API 1: GET : /school/student/personal
"student": {
"name": "value1",
"age": "value2"
}
API 2: GET : /school/student/all
"student": {
"name": "value1",
"age": "value2",
"fatherName": "value3",
"motherName": "value4",
}
For me you are using different object, you could create DTO(Data Transfer Object) and make your API return DTO.
You need adapter to transform your domine object(ex.Student) to DTO object(StudentDTO or TinyStudentDTO)
EX.
API 1: GET : /school/student/personal should return TinyStudentDTO
TinyStudentDTO
-name
-age
API 2: GET : /school/student/all should return StudentDTO
Your StudentDTO will extend with TinyStudent
StudentDTO
-fatherName
-motherName
Related
I have the sample request as given below. May I know how to make the request generates the key1, key2, etc. in JSON Swagger? As it seems like not a predefined object from the swagger file.
Sample request:
{
"products": {
"key1": {
"price": 99.146564,
"currencyPair": "USD-MYR",
"decimalPlace": 2
},
"key2": {
"price": 86.1571,
"currencyPair": "USD-IDR"
},
"key3": {
"price": 722.45654,
"currencyPair": "USD-ZNH"
}
}
}
Hope someone can guide me on how to solve it. Thanks.
I'm using Spring Rest Docs to document my REST API.
I'm using mockMVC in my integrated tests and I want to document the following JSON response:
GET /api/v1/customers/3b658b39-4264-4995-99d8-90a1672a75a7
{
"id": "3b658b39-4264-4995-99d8-90a1672a75a7",
"name": "Foo",
"nickname": "Bar",
"phones": [
{
"id": "6ca3a963-bacb-4770-a470-5902b4a17b77",
"alias": "Personal Phone 1",
"countryCode": "55",
"areaCode": "34",
"number": "99999-9999"
},
{
"id": "f3a3726b-b5f8-4652-a044-7bf3d95a37de",
"alias": "Personal Phone 2",
"countryCode": "55",
"areaCode": "34",
"number": "88888-8888"
}
]
}
How can I document the list of phones above? You could use the following snippet which uses Spring REST Docs to document this API operation:
this.mockMvc.perform(
get("/api/v1/customers/3b658b39-4264-4995-99d8-90a1672a75a7")
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("customer").withResponseFields(
fieldWithPath("id").description("Unique identifier"),
fieldWithPath("name").description("Customer full name"),
fieldWithPath("nickname").description("How the customer wants to be called")));
According to this link https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-request-response-payloads-fields-json
When documenting nested through array objects, you can use like this:
this.mockMvc.perform(
get("/api/v1/customers/3b658b39-4264-4995-99d8-90a1672a75a7")
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("customer").withResponseFields(
fieldWithPath("id").description("Unique identifier"),
fieldWithPath("name").description("Customer full name"),
fieldWithPath("nickname").description("How the customer wants to be called"),
fieldWithPath("phones[].id").description("PHONE ID DESCRIPTION"),
fieldWithPath("phones[].alias").description("PHONE ALIAS DESCRIPTION"),
fieldWithPath("phones[].countryCode").description("PHONE COUNTRY CODE DESCRIPTION"),
fieldWithPath("phones[].areaCode").description("PHONE AREA CODE DESCRIPTION")
fieldWithPath("phones[].number").description("PHONE NUMBER DESCRIPTION")
));
so I have got a problem with updating object which contain a list of elements. My object definition:
public class Project {
private String _id;
private String name;
private List<Pair> config;
}
And the Pair object:
public class Pair {
private String key;
private String value;
}
I'm using Spring Rest repository to provide the Rest Api and everything is stored in mongodb. Just using the simple interface to create mongoRepository
#RepositoryRestResource(collectionResourceRel = "project", path = "projects")
public interface ProjectRepository extends MongoRepository<Project, String>{
Project findByName(String name);
}
When I create a project object with json (sending POST to /projects):
{
"name": "test_project",
"config": [{
"key": "port",
"value": "12"
},{
"key": "port2",
"value": "123"
}]
}
I have got the proper response and object has been created:
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[
{
"key": "port",
"value": "12"
},
{
"key": "port2",
"value": "123"
}]
}
So right now I would like to send PUT to update my object and I'm getting strange results:
For example sending following body with PUT to
localhost:8151/projects/58c916fad76a3a186731ad28
{
"name": "test_project",
"config": [{
"key": "port",
"value": "12"
}]
}
So I want to remove one element from list. The response is (Status OK):
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[
{
"key": "port",
"value": "12"
},
{
"key": "port2",
"value": "123"
}]
}
So the number of elements didn't change what I expected (my expectations was that the new list replace the old one). Next test:
I would like to add one new element to list:
{
"name": "test_project",
"config": [{
"key": "port",
"value": "12"
},{
"key": "port1",
"value": "13"
},{
"key": "port2",
"value": "14"
}]
}
Gives following result:
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[
{
"key": "port",
"value": "12"
},
{
"key": "port1",
"value": "13"
}]
}
New element hasn't been added but the second element has changed.
It looks like instead of List mongo save it as an array and can't change the size but can update the element. Am I right?
But, if it would be true the next test should return the same result:
I'm sending the empty list of config and I'm expect that I will have an two-element list.
{
"name": "test_project",
"config": []
}
But what is strange for me I have got the following result:
{
"_id": "58c916fad76a3a186731ad28",
"name": "test_project",
"createdAt": "2017-03-15T10:27:06.295+0000",
"modifiedAt": "2017-03-15T10:27:06.295+0000",
"config":[]
}
So the number of elements has been updated.
To be honest right now I'm totally confused how it works. Could anyone explain how Spring rest repository handle this action and propose a proper solution for this problem?
I am having the same issue. As a workaround you can send a PATCH request. This updates the array properly.
I am using an API where I supply an input string, and it returns some keyword autocompletions and product nodes.
My goal is to deserialize the response and get a list of the autocompletion Strings I can use. I'm trying implement this in an android application with the Retrofit library, which uses gson.
First off, I'm not sure the response I have is a typical JSON response. The 'nodes' item has key / value pairs, but the input string and the autocompletions list don't seem to have keys I can use.
["pol",
["polaroid camera",
"polo",
"polo ralph lauren",
"polo ralph lauren men",
"polar heart rate monitor",
"polaroid",
"polo shirt",
"polar watch",
"police scanner",
"polar"],
[{
"nodes": [{
"alias": "electronics",
"name": "Electronics"
},
{
"alias": "electronics-tradein",
"name": "Electronics Trade-In"
}]
},
{
},
{
},
{
},
{
},
{
},
{
},
{
},
{
},
{
}],
[]]
This is my attempt at the java classes for gson to deserialize to. However, it doesn't work as from what I understand, gson needs the class variables to match the JSON keys (true for Node class but not the rest).
class Response {
String input;
List<String> keywords;
List<Node> nodes;
}
class Node {
String alias;
String name;
}
the json only has a couple of keys in it, this is largely a Json Array.
if you can change the JSON, make it more like this
{
"input" : "pol",
"keywords" : ["polaroid camera","polo",...],
"nodes": [{
"alias": "electronics",
"name": "Electronics"
},
{
"alias": "electronics-tradein",
"name": "Electronics Trade-In"
}]
}
I Have a Json which may come from other application and i need to check it whether is is in particular format. The JSON template i have is as follows,
{
"Types": {
"Type1": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3",
"recordList": {
"record1": [
{
"field": "value"
},
{
"field": {
"subrecord1": [
{
"subfield1": "subvalue1",
"subfield2": "subvalue2"
}
]
}
}
]
},
"date": "2010-08-21 03:05:03"
}
}
}
Is there any way to validate the JSON based on particular template or format.
You can use JSON Schema for that. JSON Schema lets you describe the format of the object graph you expect to receive, and then software implementing it lets you validate what you receive against your schema. There's an OSS Java implementation called json-schema-validator.