JsonParseException: Invalid JSON input. Position: 9. Character: '.' - spring

I am using MongoDB with Spring Boot 2.0.1.RELEASE. Everything seems to be working fine. I am able to perform CRUD operations properly using MongoRepository. When I use mongodb query in like
#Query(value = "{address.city:?0}")
public List<Hotel> findByCity(String city);
#Query(value = "{address.country:?0}")
public List<Hotel> findByCountry(String country);
When I try to access data using the url localhost:8090/hotels/address/city/Rome, I get the following error in response
{
"timestamp": "2018-05-04T04:51:43.549+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Invalid JSON input. Position: 9. Character: '.'.",
"path": "/hotels/address/city/rome"
}
and the following log in console:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.bson.json.JsonParseException: Invalid JSON input. Position: 9. Character: '.'.] with root cause
org.bson.json.JsonParseException: Invalid JSON input. Position: 9. Character: '.'.
I don't know why I am getting Invalid JSON input. Position: 9. Character: '.'. when I am performing GET request?
Where am I going wrong?

Missing quotes; #Query(value = "{'address.country':?0}")
– Neil Lunn
That worked for me too.

It can arise if you are missing quotes or it can be due to unparsed string passed to MongoDB. Try using parsed string like \"appname\":1. This is an example of projection.

Related

How to describe standard Spring error response in springdoc Schema?

The default server response of a SpringBoot app in case of unhandled errors is
{
"timestamp": 1594232435849,
"path": "/my/path",
"status": 500,
"error": "Internal Server Error",
"message": "this request is failed because of ...",
"requestId": "ba5058f3-4"
}
I want to describe it in a Springdoc annotation for routes of an application.
Assuming that there is a standard class DefaultErrorResponse (just a mock name), it could look like the following:
#Operation(
// ... other details
responses = {
// ... other possible responses
#ApiResponse(
responseCode = "500",
content = #Content(schema = #Schema(implementation = DefaultErrorResponse.class)))
}
)
In a worse case scenario such class does not exists and Spring uses just a Map under the hood for this response creation. Then this annotation will be more verbose, including explicit mention of every field contained in the response.
Obviously for most of the routes this part #ApiResponse(responseCode="500",... is the same, and it would be good to reduce duplication.
What is the proper way to introduce description of the default error response in the documentation?
For error Handling, you use #RestControllerAdvice in combination #ExceptionHandler, in order to refactor the error handling.
These spring annotations are scanned automatically by springdoc-openapi. Without the need to add any additional swagger annotation.

N1ql query to filter by two fields in couchbase database

I created a web service using springboot and reactive couchbase,in the repository interface i want to add two fields to the WHERE clause. What I did is below
#ViewIndexed(designDoc = "enrolment")
#N1qlPrimaryIndexed
public interface EnrolmentRepository extends ReactiveCouchbaseRepository<Enrolment,String> {
#Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and course like $1 and role like $2 within #{#n1ql.bucket}")
Mono<Enrolment> findEnrolmentByCourseRole(String course, String role);
}
But when use the above code, I get response as ,
{
"timestamp": "2019-06-14T03:57:29.670+0000",
"path": "/enrolments",
"status": 500,
"error": "Internal Server Error",
"message": "Unable to execute n1ql query due to error:{"msg":"syntax error - at role","code":3000}"
}
My endpoint is,
http://localhost:8783/enrolments?course=testcourse&role=testrole
This worked fine after changed as below.
#Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and enrolledCourse = $1 and enrolledRole = $2")
Mono<Enrolment> findEnrolmentByCourseRole(String enrolledCourse, String enrolledRole);
Seems the error was with the variable name "role"; changed everywhere to "enrolledRole".It works fine.
role is reserved keyword as described here https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/reservedwords.html
You can use back-ticks escape identifiers
Example :
SELECT *
FROM default
WHERE `role` = "xyz";

Parcing jSON Request data in graphql

Trying to do a post on a graph query in rest assured and couldn't get the query right.
Request:
"{query\":inventoryPositions(productRef:\"SKU_3\"){edges{node{onHand quantities{edges{node{ref quantity type}}}}}}}")
Response:
{
"errors": [
{
"code": "400",
"message": "Could not read document: Unexpected character ('q' (code 113)): was expecting double-quote to start field name\n
}
]
}
I have also tried this but not working:
{"query":"{inventoryPositions(productRef:\"SKU_3\"){edges{node{onHand quantities{edges{node{ref quantity type}}}}}}}"}
Please help.
basically it need 2 escape wherever u post a variable for example
"{\n"
+ "\"query\": \"{inventoryPositions(productRef: \\"SKU_3\\" locationRef: \\"locRef1\\"){edges{node{onHand quantities{edges{node{ref quantity type}}}}}}}\"\n"
+ "}"
worked for me

Fetch messages filtered by conversationId via Office365 API

I'm having some trouble figuring out how to use the office365 api to fetch messages given a conversationId.
Let's say my conversationId is AAQkADlkOGJmMTFmLTc2NjMtMKL3My04ZDhkLTVmZWNjMzA1ODY0NAAQAB11Xh2opSpBiXifMEJjhow=
I'll make a request like
https://outlook.office.com/api/v1.0/me/Messages?$filter=ConversationId%20eq%20AAQkADlkOGJmMTFmLTc2NjMtMKL3My04ZDhkLTVmZWNjMzA1ODY0NAAQAB11Xh2opSpBiXifMEJjhow=
This results in a 400 response like this:
{
"error": {
"code": "RequestBroker-ParseUri",
"message": "Syntax error at position 98 in 'ConversationId eq AAQkADlkOGJmMTFmLTc2NjMtMKL3My04ZDhkLTVmZWNjMzA1ODY0NAAQAB11Xh2opSpBiXifMEJjhow='."
}
}
I tried other things, such as url encoding the conversationId to AAQkADlkOGJmMTFmLTc2NjMtMKL3My04ZDhkLTVmZWNjMzA1ODY0NAAQAB11Xh2opSpBiXifMEJjhow%3D which results in the same error.
I also tried simply removing the = which seems to be the character that is freaking it out
https://outlook.office.com/api/v1.0/me/Messages?$filter=ConversationId%20eq%20AAQkADlkOGJmMTFmLTc2NjMtMKL3My04ZDhkLTVmZWNjMzA1ODY0NAAQAB11Xh2opSpBiXifMEJjhow
but that results in the following error
{
"error": {
"code": "RequestBroker-ParseUri",
"message": "Could not find a property named 'AAQkADlkOGJmMTFmLTc2NjMtMKL3My04ZDhkLTVmZWNjMzA1ODY0NAAQAB11Xh2opSpBiXifMEJjhow' on type 'Microsoft.OutlookServices.Message'."
}
}
I've also tried messing with the url capitalization and using + signs instead of %20 for the filter string, but I consistently get 400 errors back.
I am able to filter by other fields though. For example
https://outlook.office.com/api/v1.0/me/Messages?$filter=IsRead%20eq%20true
returns messages filtered as I would expect.
Any idea what could be going on with the ConversationId filter?
You need to wrap the ConversationId with single quotes.
This is how I forge my request in C#
string finalUrl = "https://outlook.office.com/api/beta/me/Messages?$filter=" + HttpUtility.UrlEncode(string.Format("ConversationId eq '{0}'", conversationId));

Mailchimp API 2.0 lists/subscribe responding with error 250 List_MergeFieldRequired MMERGE4 is required

I'm attempting to use the lists/subscribe Mailchimp API 2.0 endpoint to subscribe an email to a list, but I keep getting a puzzling error. My request looks like:
{
"apikey":"myapikey-us5",
"id":"listid",
"email":{"email":"my#email.com"},
"double_optin":false,
"send_welcome":true
}
I'm sending this to https://us5.api.mailchimp.com/2.0/lists/subscribe.json and getting this response:
{
"status": "error",
"code": 250,
"name": "List_MergeFieldRequired",
"error": "MMERGE4 must be provided - Please enter a value"
}
And if I specify "merge_vars": {} I still get the same error. What am I missing here?
It means you need to provide a value for MMERGE4 merge field. How you do this depends on what type of merge field MMERGE4 is, but It would be like this:
{
"apikey":"myapikey-us5",
"id":"listid",
"email":{"email":"my#email.com"},
"double_optin":false,
"send_welcome":true,
"merge_vars": {
"MMERGE4": "something"
}
}
But you should look and see what type of data that is, otherwise you might cause issues for whomever set that up as a required field.

Resources