I have to use a backend that returns me this:
{
"ok": true,
"data": [
{
"id": 0,
"name": "Intempestivo"
},
{
"id": 1,
"name": "Vacaciones"
}
}
But other times it returns me something like this:
{
"ok": true,
"data": [
{
"id": 10,
"startDate": "2020-05-29T12:00:00.000Z",
"endDate": "2020-05-30T12:00:00.000Z",
"status": "En revision",
"type": "Vacaciones"
},
{
"id": 11,
"startDate": "2020-05-29T12:00:00.000Z",
"endDate": "2020-05-30T12:00:00.000Z",
"status": "En revision",
"type": "Vacaciones"
}
]
}
How I could wrap this responses in a General Response class where the only thing that change might be the data POJO object.
public class GeneralResponse<T> {
#SerializedName("data")
private WrapperData<T> wrapperData;
#SerializedName("ok")
private boolean ok;
#SerializedName("error")
private Error error;
}
And the wrapper data is:
public class WrapperData<T> {
private T dataResponse;
public WrapperData(T POJOResponse) {
this.dataResponse = POJOResponse;
}
public T getDataResponse() {
return dataResponse;
}
}
The retrofit interface have a method like this:
#GET("permission_types/")
Call<GeneralResponse<ArrayList<PermissionType>>> getPermissionTypesWrapped();
But when I make the request I got the following exception:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 'n' column 'n' path $.data
I just solve this.
I replace private WrapperData <T> wrapperData; to private <T> wrapperDate; so when I wnat to get a list I call Call<GeneralResponse<Collection<PermissionTypes>>>
Related
I have built a controller which returns a list of objects
fun getUserCommunicationSettings(
#PathVariable commId: String,
#CommunicationTypeConstraint #RequestParam(required = false) commType: CommunicationType?
): ResponseEntity<out UserCommResponse> {
return communicationSettingsService.getUserCommSettings(commType, commId)
.mapError { mapUserCommErrorToResponse(it) }
.map { ResponseEntity.ok(SuccessResponse(it)) }
.fold<ResponseEntity<SuccessResponse<List<CommunicationSettingsDto>>>, ResponseEntity<ErrorResponse>, ResponseEntity<out UserCommResponse>>(
{ it },
{ it },
)
}
problem is it returns the following json with "entity" tag which i'd like to get rid of
{
"entity": [
{
"userId": "1075",
"userType": "CUSTOMER",
"communicationId": "972547784682",
"communicationType": "CALL",
"messageType": "CALL_COLLECTION"
}
]
}
any ideas?
I have very limited knowledge on GraphQL as I am still in the learning process. Now I stumbled upon an issue that I cannot resolve by myself without some help.
I'm using HotChocolate in my service.
I have a class ConsumerProductCategory with a Guid as Id which has a parent that is also a ConsumerProductCategory (think category > sub-category > ...)
Now I want to get the sub categories for a specific category, in linq you would write:
.Where(cat => cat.Parent.Id == id)
First of all lets start with our classes:
public class BaseViewModel : INode
{
public virtual Guid Id { get; set; }
}
public class ConsumerProductCategory : BaseViewModel
{
public ConsumerProductCategory()
{
}
public string Name { get; set; }
[UsePaging]
[UseFiltering]
[UseSorting]
public List<ConsumerProduct> Products { get; set; } = new List<ConsumerProduct>();
public ConsumerProductCategoryImage Image { get; set; }
public ConsumerProductCategory Parent { get; set; } = null;
public bool HasParent => this.Parent != null;
}
The object type definition is like this:
public class ConsumerProductCategoryType : ObjectType<ConsumerProductCategory>
{
protected override void Configure(IObjectTypeDescriptor<ConsumerProductCategory> descriptor)
{
descriptor
.Name(nameof(ConsumerProductCategory));
descriptor
.Description("Categories.");
descriptor
.Field(x => x.Id)
//.Type<UuidType>()
.Type<IdType>()
.Description($"{nameof(ConsumerProductCategory)} Id.");
descriptor
.Field(x => x.Name)
.Type<StringType>()
.Description($"{nameof(ConsumerProductCategory)} name.");
descriptor
.Field(x => x.Parent)
.Description($"{nameof(ConsumerProductCategory)} parent category.");
descriptor
.Field(x => x.Products)
.Description($"{nameof(ConsumerProductCategory)} products.");
descriptor
.ImplementsNode()
.IdField(t => t.Id)
.ResolveNode((context, id) => context.Service<IConsumerProductCategoryService>().GetByIdAsync(id));
}
}
The query to get the "main" categories would be like this:
query GetAllCategories {
consumerProductCategories(
#request: { searchTerm: "2"}
first: 10
after: null
where: { hasParent: { eq: false } }
order: {
name: ASC
}
) {
nodes {
id
name
image {
url
alt
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
This returns this result:
{
"data": {
"consumerProductCategories": {
"nodes": [
{
"id": "Q29uc3VtZXJQcm9kdWN0Q2F0ZWdvcnkKZ2EyOTYxNmRlMWMzMjQ4ZTU4YTU2YzRjYjdhMGQ5NmY5",
"name": "Category 1",
"image": {
"url": "https://picsum.photos/200",
"alt": "Category 1 Image"
}
},
{
"id": "Q29uc3VtZXJQcm9kdWN0Q2F0ZWdvcnkKZ2NmZWI0YzNiMGQyNjQyOWI4MGU0MmQ1NGNjYWE1N2Q4",
"name": "Category 2",
"image": {
"url": "https://picsum.photos/200",
"alt": "Category 2 Image"
}
},
{
"id": "Q29uc3VtZXJQcm9kdWN0Q2F0ZWdvcnkKZ2I0MjhjYWE2NGMxNTQ4MTdiMjM1ZWFhZWU3OGRhYWYz",
"name": "Category 3",
"image": {
"url": "https://picsum.photos/200",
"alt": "Category 3 Image"
}
}
],
"pageInfo": {
"endCursor": "Mg==",
"hasNextPage": false
}
}
}
}
The first thing I noticed was that the Id's (Guid's) are changed to some base64 encoded strings.
Weird, but if I would do this:
query {
node(
id: "Q29uc3VtZXJQcm9kdWN0Q2F0ZWdvcnkKZ2EyOTYxNmRlMWMzMjQ4ZTU4YTU2YzRjYjdhMGQ5NmY5"
) {
... on ConsumerProductCategory {
id
name
}
}
}
this perfectly works, result:
{
"data": {
"node": {
"id": "Q29uc3VtZXJQcm9kdWN0Q2F0ZWdvcnkKZ2EyOTYxNmRlMWMzMjQ4ZTU4YTU2YzRjYjdhMGQ5NmY5",
"name": "Category 1"
}
}
}
However, now I want to filter on the Parent.Id,
query GetSubcategories {
consumerProductCategories(
first: 10
after: null
where: { parent: { id: { eq: "Q29uc3VtZXJQcm9kdWN0Q2F0ZWdvcnkKZ2NmZWI0YzNiMGQyNjQyOWI4MGU0MmQ1NGNjYWE1N2Q4"}} }
order: {
name: ASC
}
) {
nodes {
id
name
image {
url
alt
}
parent {
id
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
This gives an error that the fieldtype where I do the "eq" is not correct, makes sense because in the data it's actually a Guid.
The result:
{
"errors": [
{
"message": "The specified value type of field `eq` does not match the field type.",
"locations": [
{
"line": 5,
"column": 31
}
],
"path": [
"consumerProductCategories"
],
"extensions": {
"fieldName": "eq",
"fieldType": "UUID",
"locationType": "UUID",
"specifiedBy": "http://spec.graphql.org/June2018/#sec-Values-of-Correct-Type"
}
}
]
}
I understand why it gives me this error, but I have no clue how to resolve this.
I looked everywhere on Google but have not found a similar question and in the official docs of HotChocolate I cannot really find a solution for this issue.
Can anyone point me in the right direction?
By the way, is it a good practice to use these "autogenerated" base64 strings as Id's, or is there some way to specify that this generation should not happen and actually return the Guid's instead?
Thanks in advance!
Ok, I can answer my own question, basically it isn't supported yet: github
What I've done for now is just add a second Guid in the base class:
This results in:
Make a simple RestController
#RestController
public class Controloler
#Value
class MyData {
int value;
}
#GetMapping(value = "/datas", produces = MediaTypes.HAL_JSON_VALUE)
public PagedResources<Resource<MyData>> getMyData(PagedResourcesAssembler<MyData> assembler,
#RequestParam(required = false) String param,
#PageableDefault Pageable pageRequest)
{
MyData data = new MyData(1);
Page<MyData> page = new PageImpl<>(Collections.singletonList(data), pageRequest, 100);
Link selfLink = linkTo(methodOn(Controloler.class).getMyData(assembler, param, pageRequest)).withSelfRel();
return assembler.toResource(page, selfLink);
}
}
When I try to get page curl "http://localhost:8080/datas?param=12&page=2" have a problem with self link generation
{
"_embedded": {
"myDataList": [
{
"value": 1
}
]
},
"_links": {
"first": {
"href": "http://localhost:8080/datas?param=12&page=0&size=10"
},
"prev": {
"href": "http://localhost:8080/datas?param=12&page=1&size=10"
},
"self": {
"href": "http://localhost:8080/datas?param=12"
},
"next": {
"href": "http://localhost:8080/datas?param=12&page=3&size=10"
},
"last": {
"href": "http://localhost:8080/datas?param=12&page=9&size=10"
}
},
"page": {
"size": 10,
"totalElements": 100,
"totalPages": 10,
"number": 2
}
}
In my opinion, self link should be http://localhost:8080/datas?param=12&page=2&size=10.
Just now I can solve this problem without using pageable in arguments, just exact params page and size. But, I hope there is some solution with pageable
I've seen that in case of spring-data-rest self have a type of template. But I'd like to get the url I've requested
In my opinion, self link should be http://localhost:8080/datas?param=12&page=2&size=10.
I agree. In fact, it seems to be a bug. The most recent version of PagedResourcesAssembler does it differently:
Link selfLink = link.map(it -> it.withSelfRel())//
.orElseGet(() -> createLink(base, page.getPageable(), Link.REL_SELF));
(source)
Buggy versions of that class are doing this:
resources.add(createLink(base, null, Link.REL_SELF));
The createLink method is never passed the needed Pageable, but null as the second argument.
So, if you can't upgrade to the most recent version you can still work-around it:
Link selfLink = linkTo(methodOn(Controloler.class).getMyData(assembler, param, pageRequest)).withSelfRel();
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(selfLink.expand().getHref());
new HateoasPageableHandlerMethodArgumentResolver().enhance(builder, null, pageRequest);
Link newSelfLink = new Link(builder.build().toString());
According to Oliver's comment in an issue opened to address this, the self link should not contain template information, and this is not a bug.
I'm working with an API rest and it returns me two types of json, object and array.
Object(When there is only one record in the database):
{ "complain": { "id": "1" , "description": "hello", "date": "2017-01-24 11:46:22", "Lat": "20.5204446", "Long": "-100.8249097" } }
Array(When there is more than one record in the database):
{ "complain": [ { "id": "1" , "description": "hello", "date": "2017-01-24 11:46:22", "Lat": "20.587446", "Long": "-100.8246490" }, { "id": "2" , "description": "hello 2", "date": "2017-01-24 11:50:12", "Lat": "20.529876", "Long": "-100.8249097" } ] }
The code I use to consume the json is as follows:
content = await response.Content.ReadAsStringAsync();
var token = JToken.Parse(content);
if (token["complain"] is JArray)
{
var jsonArray = JsonConvert.DeserializeObject<RootArray>(content);
}
else if (token["complain"] is JObject)
{
var jsonObject = JsonConvert.DeserializeObject<RootObject>(content);
}
When it comes to a json array if I can add it to a listview:
myList.ItemsSource = jsonArray.listArray;
But if it is an object I can not and I get the following error:
Cannot implicitly convert type Object to IEnumerable.
Finally I was able to solve my error, it was just a matter of creating a list and adding the deserialized json object.
var jsonObject = JsonConvert.DeserializeObject<RootObject>(content);
List<Complain> simpleList = new List<Complain>();
simpleList.Add(jsonObject.ComplainObject);
myList.ItemsSource = simpleList;
The class to deserialize the Json object:
public class RootObject
{
[JsonProperty("complain")]
public Complain ComplainObject { get; set; }
}
I need somehow to store metadata in the can.Model
I use findAll method and receive such JSON:
{
"metadata": {
"color": "red"
},
"data": [
{ "id": 1, "description": "Do the dishes." },
{ "id": 2, "description": "Mow the lawn." },
{ "id": 3, "description": "Finish the laundry." }
]
}
I can work with data like can.Model.List, but I need metadata like a static property or something.
You can use can.Model.parseModels to adjust your response JSON before it's turned into a can.Model.List.
parseModels: function(response, xhr) {
var data = response.data;
var metadata = response.metadata;
var properties;
if(data && data.length && metadata) {
properties = Object.getOwnPropertyNames(metadata);
can.each(data, function(datum) {
can.each(properties, function(property) {
datum[property] = metadata[property];
});
});
}
return response;
}
Here's a functional example in JS Bin: http://jsbin.com/qoxuju/1/edit?js,console