Error while extracting field using JsonPath in Scala - scala

I am trying to extract a data field from a Json string using JsonPath and I am facing a wired problem:
Json string:
val message = {"me":"a","m":"1.0","message_metadata":"{"massage":"12542","start_date":"1515"}"}
Code:
val eventMetaData = JsonPath.read[String](message, "$.message_metadata")
When I run this from a host it throws an class not found exception:
Exception:
java.lang.NoClassDefFoundError: com/jayway/jsonpath/Predicate
at com.amazon.isp.execution.notifications.messageHandlers.CosmosNotificationsHandler$$anonfun$2.apply(CosmosNotificationsHandler.scala:164) ~[ScreeningExecutionEngineService-1.0.jar:?]
at com.amazon.isp.execution.notifications.messageHandlers.CosmosNotificationsHandler$$anonfun$2.apply(CosmosNotificationsHandler.scala:153) ~[ScreeningExecutionEngineService-1.0.jar:?]
at scala.util.Try$.apply(Try.scala:192) ~[scala-library.jar:?]
at com.amazon.isp.execution.notifications.messageHandlers.CosmosNotificationsHandler.getEventType(CosmosNotificationsHandler.scala:153) ~[ScreeningExecutionEngineService-1.0.jar:?]
at com.amazon.isp.execution.notifications.messageHandlers.CosmosNotificationsHandler.processMessage_aroundBody0(CosmosNotificationsHandler.scala:63) ~[ScreeningExecutionEngineService-1.0.jar:?]
at com.amazon.isp.execution.notifications.messageHandlers.CosmosNotificationsHandler$AjcClosure1.run(CosmosNotificationsHandler.scala:1) ~[ScreeningExecutionEngineService-1.0.jar:?]
at org.a
I not using any Predicate class but still get the error. Please let me know if you faced this kind of problem before.

Do you have this library in your build.sbt or your pom.xml like this?
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.9.1</version>
</dependency>

Related

Spark 3.0 scala.None$ is not a valid external type for schema of string

While using elasticsearch-hadoop library for reading elasticsearch index with empty attribute, getting the exception
Caused by: java.lang.RuntimeException: scala.None$ is not a valid external type for schema of string
There is open defect in github for the same with steps to reproduce it: https://github.com/elastic/elasticsearch-hadoop/issues/1635
Spark: 3.1.1
Elasticsearch-Hadoop : elasticsearch-spark-30_2.12-7.12.0
Elasticsearch : 2.3.4
It worked by setting elasticsearch-hadoop property es.field.read.empty.as.null = no
.option("es.field.read.empty.as.null", "no")
From Elasticsearch Link:
es.field.read.empty.as.null (default yes)
Whether elasticsearch-hadoop will treat empty fields as null.

Need help in REST ASSURED

I am starting with REST Assured, getting error while executing below code :
Code 1-
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method equalTo(String) is undefined for the type
Code 2 -
RestAssured.expect().statusCode(200).
body(
"name", Matchers.equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
Below are the only 2 methods in my class, I am having issue with first one, second one is running fine. Please let me know what I am missing in first method.
Method -1
public static void testCountriesCallingCode() {
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7").asString());
}
Method-2
public static void testCountriesCallingCodeUsingJSONPATH(){
Response res = RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(res.getStatusCode());
String json = res.asString();
JsonPath jp = new JsonPath(json);
System.out.println(jp.get("name"));
}
Thanks Hti, your answer worked. Without the other dependencies, Rest Assured kind of works. I have no idea why Rest Assured website does not note this. Following in pom.xml worked
<properties>
<rest-assured.version>3.0.2</rest-assured.version>
<resteasy.version>3.0.17.Final</resteasy.version>
</properties>
...
<!-- Jackson is for allowing you to convert pojo (plain old Java object) into JSON -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>2.4.11</version>
<scope>test</scope>
</dependency>
Change the body of your first example to:
body(
"[0].name", equalTo("Russia")
)
That is because the JSON response from the server is not an object, but an array, and you have to query for the first object ([0]), then the name (.name).
For the Code-1, for equalTo() method you have to import org.hamcrest.Matchers.*;
For the exception in code 2, it is very hard to mention without looking at the RESPONSE but try to follow below link if you have nested generic parameters in your response.
How to validate nested response using REST Assured?
Please let me know if you have any issue or question. Thanks!
Even though this question is old, I just stumpled upon the second problem:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
This is due to missing dependencies. In my case I needed to add the dependencies for xml-path and groovy-xml, even though I'm just working with JSON data. So the best thing to do is resolving the dependencies transitively.
equalTo comes from Hamcrest which is a JUnit dependency contained within the JUnit jar. You probably just need to import the static method for it from Hamcrest.
import static org.hamcrest.core.IsEqual.*;
Add a static package for equal to:
import static org.hamcrest.Matchers.*;

Unable to add label using Neo4j Rest API - Error reading as JSON ''

The neo4j rest api throws runtime exception (error reading as JSON '') when trying to add a label.
My current set up
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.0.0</version>
</dependency>
Code that tries to create a new node and add a property and label. The runtime exception is thrown when we try to add the label. The rollback is working fine though. It appears that the API is trying to get details for the resource that is not yet created and trying to parse the response.
try ( Transaction tx = db.beginTx() ) {
//create new user
Node userNode = db.createNode();
userNode.setProperty( "id", id );
userNode.addLabel(DynamicLabel.label("GuestUser")); //throws runtime exception
tx.success();
}
Stack trace
java.lang.RuntimeException: Error reading as JSON ''
at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:57)
at org.neo4j.rest.graphdb.util.JsonHelper.jsonToSingleValue(JsonHelper.java:62)
at org.neo4j.rest.graphdb.RequestResult.toEntity(RequestResult.java:114)
at org.neo4j.rest.graphdb.RequestResult.toMap(RequestResult.java:120)
at org.neo4j.rest.graphdb.ExecutingRestAPI.getData(ExecutingRestAPI.java:501)
at org.neo4j.rest.graphdb.RestAPIFacade.getData(RestAPIFacade.java:179)
at org.neo4j.rest.graphdb.entity.RestEntity.getStructuralData(RestEntity.java:75)
at org.neo4j.rest.graphdb.entity.RestNode.labelsPath(RestNode.java:188)
at org.neo4j.rest.graphdb.entity.RestNode.addLabel(RestNode.java:147)
Caused by: java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
...
at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:55)
... 43 more
Has anyone seen this problem so far.
This is a problem as those "pseudo" transactions only aggregate operations to send them at once at commit.
So you cannot do "read your writes" or make decisions on them.
And the addLabel operation uses the path returned from the structural info of the node which does not yet exist.
Don't think it's worth fixing. If you think so, please raise an issue at https://github.com/neo4j/java-rest-binding/issues

AxisFault: javax.xml.stream.XMLStreamException

we are using drupal soapservice for webservices. while we are trying to call soapservice, it returns the results but axis2 throws the following error...
org.apache.axis2.AxisFault: javax.xml.stream.XMLStreamException: element text content may not contain START_ELEMENT
at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
at com.chm.api.login.client.DrupalSoapStub.fromOM(DrupalSoapStub.java:12249)
at com.chm.api.login.client.DrupalSoapStub.userLogin(DrupalSoapStub.java:1408)
at SocketClient.main(SocketClient.java:144)
Caused by: java.lang.Exception: javax.xml.stream.XMLStreamException: element text content may not contain START_ELEMENT
at com.chm.api.login.client.DrupalSoapStub$UserLoginResponse$Factory.parse(DrupalSoapStub.java:59804)
at com.chm.api.login.client.DrupalSoapStub.fromOM(DrupalSoapStub.java:11473)
... 2 more
Caused by: javax.xml.stream.XMLStreamException: element text content may not contain START_ELEMENT
at org.apache.axiom.om.impl.SwitchingWrapper.getElementText(SwitchingWrapper.java:981)
at javax.xml.stream.util.StreamReaderDelegate.getElementText(StreamReaderDelegate.java:100)
at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.getElementText(XMLStreamReaderWrapper.java:100)
at org.apache.axiom.util.stax.debug.XMLStreamReaderValidator.getElementText(XMLStreamReaderValidator.java:76)
at com.chm.api.login.client.DrupalSoapStub$UserLoginResponse$Factory.parse(DrupalSoapStub.java:59770)
I had faced a similar issue, but not exactly the same as you are facing. I had resolved the issue by adding the following dependency in the class path:
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.5</version>
</dependency>
I am not sure whether it works for your problem or not, but you can try it.

DuplicateMemberException play framework mongodb module

I get this error on starting play application. Im using Play 1.2.4 and Mongo 1.3 module.
`Oops: DuplicateMemberException
An unexpected error occured caused by exception DuplicateMemberException: duplicate method: getCollectionName in models.MongoShapes
play.exceptions.UnexpectedException: While applying play.modules.mongo.MongoPlugin#2d7cec96 on models.MongoShapes
at play.plugins.PluginCollection.enhance(PluginCollection.java:511)
at play.classloading.ApplicationClasses$ApplicationClass.enhance(ApplicationClasses.java:235)
at play.classloading.ApplicationClassloader.loadApplicationClass(ApplicationClassloader.java:165)
at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:429)
at play.Play.start(Play.java:505)
at play.Play.detectChanges(Play.java:618)
at play.Invoker$Invocation.init(Invoker.java:198)
at Invocation.HTTP Request(Play!)
Caused by: javassist.bytecode.DuplicateMemberException: duplicate method: getCollectionName in models.MongoShapes
at javassist.bytecode.ClassFile.testExistingMethod(ClassFile.java:593)
at javassist.bytecode.ClassFile.addMethod(ClassFile.java:577)
at javassist.CtClassType.addMethod(CtClassType.java:1235)
at play.modules.mongo.MongoEnhancer.enhanceMongoEntity(MongoEnhancer.java:69)
at play.modules.mongo.MongoEnhancer.enhanceThisClass(MongoEnhancer.java:35)
at play.modules.mongo.MongoPlugin.enhance(MongoPlugin.java:17)
at play.plugins.PluginCollection.enhance(PluginCollection.java:506)
... 7 more
My class looks like
#MongoEntity
public class MongoShapes extends MongoModel{
String someComment;
}
Looks like your project include mongo module more than once. How did you declare mongo module? don't declare it in both applicaiton.conf and dependencies.yml.