if the size is larger than the number of records, my object Page returns empty.
parameters: size=100&page=1
{
"content": [],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"offset": 100,
"pageSize": 100,
"pageNumber": 1,
"paged": true,
"unpaged": false
},
"totalElements": 69,
"totalPages": 1,
"last": true,
"size": 100,
"number": 1,
"numberOfElements": 0,
"first": false,
"sort": {
"sorted": false,
"unsorted": true,
"empty": true
},
"empty": true
}
How to return only the records found?
tks!
As I know, in the Spring Data, by default, the page number is started from 0.
And you try to get page=1 (this is the second page).
Related
I have a GET endpoint in my application supposed to return :
{
"gameId": "41a483c4-6220-424a-a931-d9114a4f6748",
"pits": [
{
"id": 1,
"stones": 6
},
{
"id": 2,
"stones": 6
},
{
"id": 3,
"stones": 6
},
{
"id": 4,
"stones": 6
},
{
"id": 5,
"stones": 6
},
{
"id": 6,
"stones": 6
},
{
"id": 7,
"stones": 0
},
{
"id": 8,
"stones": 6
},
{
"id": 9,
"stones": 6
},
{
"id": 10,
"stones": 6
},
{
"id": 11,
"stones": 6
},
{
"id": 12,
"stones": 6
},
{
"id": 13,
"stones": 6
},
{
"id": 14,
"stones": 0
}
],
"playerTurn": null,
"currentPitIndex": 0
}
but instead it returns:
{
"id": "25f09303-b797-418f-a7e7-db0e5fa8631b",
"pits": [
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
}
],
"playerTurn": null,
"currentPitIndex": null
}
I am wondering what is "empty"?! and where is "id"!
would be much appreciated for any suggestion and help.
thank you
Does your class have a method called isEmpty()? Because most JSON marshalling frameworks add JSON properties for any method with 0 arguments that starts with get, or returns a boolean and starts with is. For the same reason id is probably missing - I'm guessing there is no getter for it. The top level gameId is probably called id because you have a getId() method, not a getGameId() method.
For most frameworks you can tweak this with annotations. For instance, with Jackon you can use #JsonIgnore to indicate a method should not be represented as a JSON property, and #JsonProperty can be used to a) add a custom name, or b) allow a field to be included as well. For other frameworks you should check their documentation.
The following JSON Object (a variable named json_object) is composed of two values
peripheral_devices
peripheral_tests
both of which are arrays:
{
"peripheral_devices": [
{
"_id": 1,
"active": -1,
"battery": "63",
"bt_firmware_version": "btv1.7.777",
"configured": 0,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323224,
"device_id": 72,
"discovered": 0,
"firmware_version": "v3.14",
"hardware_version": "null",
"icon": "icon_device_bp5",
"last_connect_date": 1640023710420,
"mac_address": "8C:DE:52:41:FC:57",
"model": "BP5 41FC57",
"name": "BP5",
"other_id": "-1",
"paired": -1,
"type_id": 1,
"update_date": 1635807323224,
"user_id": 13
},
{
"_id": 3,
"active": -1,
"battery": "90",
"bt_firmware_version": "1.0.0",
"configured": -1,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323239,
"device_id": 72,
"discovered": 0,
"firmware_version": "1.0.0",
"hardware_version": "5.0.0",
"icon": "icon_device_hs2s",
"last_connect_date": 1640022147928,
"mac_address": "00:4D:32:0C:B7:2C",
"model": "HS2S 11070",
"name": "HS2S",
"other_id": "-1",
"paired": -1,
"type_id": 3,
"update_date": 1635807323239,
"user_id": 13
}
],
"peripheral_tests": [
{
"_id": 199,
"consumer_id": 22,
"create_date": 1640020949760,
"end_date": 1640020949078,
"mood_id": -1,
"notes": "",
"start_date": 1640020939533,
"status_id": 1,
"type_id": 2,
"update_date": 0,
"user_id": -99
},
{
"_id": 198,
"consumer_id": 22,
"create_date": 1640020904183,
"end_date": -1,
"metric_bps": {,
"mood_id": -1,
"notes": "",
"start_date": 1640020863742,
"status_id": 1,
"type_id": 1,
"update_date": 0,
"user_id": -99
},
{
"_id": 197,
"consumer_id": 22,
"create_date": 1640020834664,
"end_date": 1640020828741,
"mood_id": -1,
"notes": "",
"start_date": 1640020822580,
"status_id": 1,
"type_id": 3,
"update_date": 0,
"user_id": -99
}
]
}
and derived as follows:
JSONObject json_object = new JSONObject(post_data);
Using org.json.JSONArray.JSONArray:
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_devices");
works as expected and a json array is parsed from json_object. However, using the same technique to get the second array in the object does not:
JSONArray json_tests = new JSONArray("peripheral_tests");
throws exception:
A JSONArray text must start with '[' at 1 [character 2 line 1]
Why? What am I not understanding?
You code
JSONArray json_tests = new JSONArray("peripheral_tests");
Take string in the constructor which is the JSON data and peripheral_tests is not JSON array, to get peripheral_tests you need to get it from json_object like
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_tests");
The input JSON is invalid, accoring to JSON Lint ...which may lead to unexpected results:
Error: Parse error on line 69:
..."metric_bps": { , "mood_id": -1,
----------------------^
Expecting 'STRING', '}', got ','
Got the following JSON:
{
"content": [
7,
8,
9,
10
],
"last": true,
"total_elements": 9,
"total_pages": 2,
"first": false,
"number_of_elements": 4,
"size": 5,
"number": 1,
"empty": false
}
and I would like to create a contract with PACT using the following code:
DslPart body = newJsonBody((root) -> {
root.array("consumer", a -> a.integerType().integerType().integerType())
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty");
}).build();
Although this is working, I really do not like the array contract using a concatenation of integerType.
My question is: is there a better way to indicate an array of X elements, which have to be of type Integer?
You can use minArrayLike.
#Test
public void test() {
DslPart body = newJsonBody((root) ->
root.minArrayLike("content", 4, PactDslJsonRootValue.integerType(1), 4)
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty"))
.build();
System.out.println(body.toString());
}
Produces
{
"content": [
1,
1,
1,
1
],
"number": 100,
"last": true,
"size": 100,
"total_elements": 100,
"total_pages": 100,
"number_of_elements": 100,
"first": true,
"empty": true
}
I have a simple Spring Boot application that has the next method inside my controller:
#RequestMapping(method = RequestMethod.GET, value = "/")
public ResponseEntity<Page> getUsers(Pageable pageable) {
Page<User> users = repository.findAll(pageable);
return ResponseEntity.ok(users);
}
My records are being fetched using simple pageable repository. The response generated is:
{
"content": [
{
...
},
{
...
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 20,
"paged": true,
"unpaged": false
},
"totalPages": 1,
"totalElements": 3,
"last": true,
"size": 20,
"number": 0,
"sort": {
"sorted": false,
"unsorted": true
},
"numberOfElements": 3,
"first": true
}
How do I achieve same result as when I use ResourceSupport, which is:
.....
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
Reason is - I do not need the links generation and so do not want to deal with the ResourceSupport/Assembler facilities of hateoas.
Is there any built-in option (besides writing my own serialization for Page < T >) ?
I am currently in the process of attempting to update an ElasticSearch document via the Java API. I have a groovy script with the following code:
static updateRequestById(String agencyIndex, String type, String id, def policy) {
UpdateRequest updateRequest = new UpdateRequest()
updateRequest.docAsUpsert(true);
updateRequest.parent("agentNumber");
updateRequest.index(agencyIndex)
updateRequest.type(type)
updateRequest.id(id)
updateRequest.doc("policies", policy)
elasticsearchClient.update(updateRequest).get()
}
The problem with I am having is that I want to update an array within the following document:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "int-b-agency",
"_type": "jacket",
"_id": "99808.1.27.09_4644",
"_score": 1,
"_source": {
"agentNumber": "99808.1.27.09",
"fileNumber": "4644",
"policies": [
{
"agentNumber": "99808.1.27.09",
"fileNumber": "4644",
"policyNumber": "2730609-91029084",
"checkNumber": "0",
"checkAmount": 0,
"createdOn": null,
"createdBy": "traxuser621",
"propertyTypeCode": "",
"propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
"ppaddress": "110 Allan Ct ",
"ppcity": "Jacksonville",
"ppstate": "FL",
"ppzip": "32226",
"ppcounty": "Duval",
"policytype": "",
"status": "Active",
"effectiveDate": "2015-04-01T00:00:00-05:00",
"formType": "BASIC OWNERS - ALTA Owners Policy 06_306_FL - FL Original Rate",
"rateCode": "FLOR",
"rateCodeDesc": "FL Original Rate",
"policyTypeCode": "1",
"policyTypeCodeDesc": "BASIC OWNERS",
"amount": 200000,
"hoiAgentNumber": "",
"proForma": false,
"pdfLocation": "\\\\10.212.61.206\\FNFCenter\\legacy_jacket_pdfs\\2015_4_FL6465\\Policy_2730609-91029084.pdf",
"legacyPolicy": "true",
"associatedPolNbr": null
}
]
}
}
]
}
}
In the document above I have a document that has an array called "policies" with a single object. I want to be able to update the "policies" array with additional objects. The end result should look something like the following:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "int-b-agency",
"_type": "jacket",
"_id": "41341.1.81.38_41340103",
"_score": 1,
"_source": {
"agentNumber": "41341.1.81.38",
"fileNumber": "41340103",
"policies": [
{
"agentNumber": "41341.1.81.38",
"fileNumber": "41340103",
"policyNumber": "8122638-91036874",
"checkNumber": "0",
"checkAmount": 0,
"createdOn": null,
"createdBy": "traxuser621",
"propertyTypeCode": "",
"propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
"ppaddress": "1800 Smith St ",
"ppcity": "sicklerville",
"ppstate": "PA",
"ppzip": "08105",
"ppcounty": "Dauphin",
"policytype": "",
"status": "Active",
"effectiveDate": "2016-02-01T00:00:00-06:00",
"formType": "TestData",
"rateCode": "PASALERATE",
"rateCodeDesc": "Sale Rate - Agent",
"policyTypeCode": "26",
"policyTypeCodeDesc": "SALE OWNERS",
"amount": 180000,
"hoiAgentNumber": "",
"proForma": false,
"pdfLocation": "SomeLocation1",
"legacyPolicy": "true",
"associatedPolNbr": null
},
{
"agentNumber": "41341.1.81.38",
"fileNumber": "41340103",
"policyNumber": "8122638-91036875",
"checkNumber": "0",
"checkAmount": 0,
"createdOn": null,
"createdBy": "traxuser621",
"propertyTypeCode": "",
"propertyTypeDesc": "1-4 FAMILY RESIDENTIAL",
"ppaddress": "1800 Smith St ",
"ppcity": "sicklerville",
"ppstate": "PA",
"ppzip": "08105",
"ppcounty": "Dauphin",
"policytype": "",
"status": "Active",
"effectiveDate": "2016-02-01T00:00:00-06:00",
"formType": "Test Data",
"rateCode": "PASALERATE",
"rateCodeDesc": "Sale Rate - Agent",
"policyTypeCode": "26",
"policyTypeCodeDesc": "SALE OWNERS",
"amount": 180000,
"hoiAgentNumber": "",
"proForma": false,
"pdfLocation": "SomeLocation2",
"legacyPolicy": "true",
"associatedPolNbr": null
}
]
}
}
]
}
}
What am I doing wrong?
You can use a scripted update:
Put your new policy in a parameter, for example policy
Use a script like the following :
if (!ctxt._source.policies) { ctxt._source.policies = [] }
ctxt._source.policies += policy
See this documentation : https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
Updates in inverted indexes are deletes and replacements of documents. There is no in-place update like you find in a db. ES uses Lucene under the hood which in-turn implements a kick-ass inverted index.