Issue Sending Request from gRPC Java Client - java

I have this code:
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090).usePlaintext().build();
SentenceClassificationServiceGrpc.SentenceClassificationServiceBlockingStub SentenceClassificationStub = SentenceClassificationServiceGrpc.newBlockingStub(channel);
SentenceClassificationRequest request = SentenceClassificationRequest.newBuilder().setDocument("hey").setThreshold(1);
SentenceClassificationResponse response = SentenceClassificationStub.classification(request);
and I keep on getting this error:
java: incompatible types: com.application.SentenceClassificationRequest.Builder cannot be converted to com.application.SentenceClassificationRequest
I've generated the gRPC Java files using the Maven plugin. After looking at multiple examples I'm not sure what my issue is.

Solution
You forgot to build your SentenceClassificationRequest, currently you're assigning your SentenceClassificationRequest.Builder to SentenceClassificationRequest
Just add .build() to the end of the builder
SentenceClassificationRequest request = SentenceClassificationRequest.newBuilder().setDocument("hey").setThreshold(1).build();

Related

Using ES's UpdateByQueryRequestBuilder with Rest High Level Client

In Elasticsearch v5.5, we have used Transport Client when defining
UpdateByQueryRequestBuilder and it worked fine:
UpdateByQyeryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE
.newRequestBuilder(transportClient);
Since we're upgrading to use RestHighLevelClient, the above builder no longer works and it has this as error: "The method newRequestBuilder(ElasticsearchClient) in the type UpdateByQueryAction is not applicable for the arguments (RestHighLevelClient)".
Does anyone know if i can just simply cast it like below:
UpdateByQyeryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE
.newRequestBuilder((ElasticsearchClient) restHighLevelClient);
or there should be some other way to do it? Thanks
From the documentation. It looks like you should prepare request directly:
UpdateByQueryRequest request = new UpdateByQueryRequest("source1", "source2");
request.set...
and later execute the request:
BulkByScrollResponse bulkResponse = client.updateByQuery(request, RequestOptions.DEFAULT);
I think UpdateByQyeryRequestBuilder is a class specific for the TransportClient only.

How to manipulate Play Framework WS response from java controller

In Latest Play JAVA (v 2.6) I am trying to fetch 3rd party restful api and process the response for further calculation in the controller. As the response type is a CompletionStage I am unable to convert it to usable JSON string from the response.
What I have tried,
final WSResponse r = (WSResponse) ws.url(domainUrl).setRequestTimeout(5000).get();
final JsonNode result = r.asJson();
But no help.
I also tried to fetch using java HttpURLConnection but there is no help either as the request is stopping for ssl skipping error, which only can be solved from play configuration.
Advance thanks!
Use plain Jackson to parse the body String:
final WSResponse r = ...;
Json.mapper().readValue(r, Type.class)

Restarting app server using AWS API

I need to restart my AWS app server, for this I tried to use AWS API and have done the following:
1) Used the aws java sdk maven repository
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-elasticbeanstalk</artifactId>
<version>1.11.86</version>
</dependency>
2) Used the below code segment:
AWSElasticBeanstalk client = new AWSElasticBeanstalkClient();
RestartAppServerRequest request = new RestartAppServerRequest()
.withEnvironmentId("<myEnvId>")
.withEnvironmentName("<myEnvName>");
RestartAppServerResult response = client.restartAppServer(request);
I get the below error:
com.amazonaws.services.elasticbeanstalk.model.AWSElasticBeanstalkException: No Environment found for EnvironmentId = ''. (Service: AWSElasticBeanstalk; Status Code: 400; Error Code: InvalidParameterValue; Request ID: 4d025449-ed00-11e6-8405-4d5eb8e5ecd9)
The <myEnvId> and <myEnvName> are correct as they are taken from the AWS dashboard.
I also tried including the aws.accessKeyId and aws.secretKey to java system properties. Still I get the same error.
Is there something I am missing or doing wrong? Please advice.
Thanks,
Clyde
It sounds like you need to configure the region. For example to configure the region to us-west-2 you would use the following code:
AWSElasticBeanstalk client = new AWSElasticBeanstalkClient();
client.configureRegion(Regions.US_WEST_2);
Thanks to all who posted. I manged to solve the issue. The code segment used is as follows:
AWSElasticBeanstalk client = new AWSElasticBeanstalkClient();
client.setEndpoint(<set your endpoint>);
RestartAppServerRequest request = new RestartAppServerRequest()
.withEnvironmentId(<set your env id>)
.withEnvironmentName(<set your env name>);
RestartAppServerResult response = client.restartAppServer(request);
This worked find.

Amazon Product Advertising API request with Java

Hi My code for the same is
// Initialize Web Service
HandlerResolver handlerResolver=new AwsHandlerResolver(credentials.getAWSSecretKey());
AWSECommerceService service = new AWSECommerceService();
service.setHandlerResolver(handlerResolver);
// Create Web Service Connection
AWSECommerceServicePortType port = service.getAWSECommerceServicePort();
// Add Parameters for the Item Lookup
ItemLookupRequest itemLookup = new ItemLookupRequest();
itemLookup.setIdType("ASIN");
itemLookup.getItemId().add("B000RE216U");
// Wrap Request in Lookup Body
ItemLookup lookup = new ItemLookup();
lookup.setAWSAccessKeyId(credentials.getAWSAccessKeyId());
lookup.getRequest().add(itemLookup);
ItemLookupResponse response = port.itemLookup(lookup);
System.out.println("response: " + response.toString());
I keep getting the error cannot convert from Void to AWSECommerceService in the beginning. I have the AWSHandlerResolver file and codec jar installed and configured.
Error Message:
Exception in thread "main" javax.xml.ws.WebServiceException: {http://webservices.amazon.com/AWSECommerceService/2010-11-01}AWSECommerceService is not a valid service. Valid services are: {http://webservices.amazon.com/AWSECommerceService/2011-08-01}AWSECommerceService
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:223)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:168)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:96)
at javax.xml.ws.Service.<init>(Service.java:77)
at com.ECS.client.jax.AWSECommerceService.<init>(AWSECommerceService.java:46)
I have been searching online. I might have to change target namespace for AWSECommerceService. But cannot find how. Please help me
You are using the wrong namespace (actually, the wrong version of WS) for your Webservice client and its port.
Go to AWSECommerceService and AWSECommerceServicePortType classes and replace all namespaces which look like http://webservices.amazon.com/AWSECommerceService/2010-11-01 with http://webservices.amazon.com/AWSECommerceService/2013-08-01.

Can web service testing done using SOAPUI tool be done in Java completely?

In SOAP UI Web service testing,
User imports the Project in to the work space and mentions the End point. Enters the required data in the Request xml and runs to get the resulting response in xml format.
Is there a way we can achieve this using Java only without using the SoapUI tool. I guess the steps should be:
Create a Wsdl Project.
Create a xml request (in the required format)
Send the request to the end point (How to do this?)
Receive response and verify it.
Please help me how to do this using Java only(without using SOAP UI tool). Any links/code will be greatly helpful.
Thanks,
Mike
Use soapUI API.
;
Here are some useful links:
http://www.soapui.org/Developers-Corner/integrating-with-soapui.html
http://pritikaur23.wordpress.com/2013/06/16/saving-a-soapui-project-and-sending-requests-using-soapui-api/
I used the following code to create a project:
File projectFile = new File(filePath);
SoapUI.setSoapUICore(new StandaloneSoapUICore(true));
WsdlProject project = new WsdlProject();
project.setName(projectName);
WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, url);
for (WsdlInterface wsdl : wsdls) {
int c = wsdl.getOperationCount();
String reqContent = "";
for (int j = 0; j < c; j++) {
WsdlOperation op = wsdl.getOperationAt(j);
reqContent = op.createRequest(true);
WsdlRequest req = op.addNewRequest(requestName);
req.setRequestContent(reqContent );
}
}
project.saveIn(projectFile);
SoapUI.shutdown();
You can create client and pass in HTTP Request test request populated with needed parameter for testing purpose, below mention question has some useful insights.
Java Web service testing

Categories