access web services from WSDL link java - java

I am trying to access web services that are defined in WSDL by giving the url of the WSDL. The specific case I am working on is the ebay "FindingService".
After parsing the WSDL, I search for the service I am looking for (for example "FindingService"). Next, I want to be able to use that service (send keywords and get results) through some sort of client. I looked around and found the following code that I tried to modify to use it for my example. Since I am still new to WSDL, I am not sure of how to adapt it and I keep getting the error: Undefined port type: {http://WSDL/}face
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class client{
public static void main(String[] args) throws Exception {
URL url = new URL("http://developer.ebay.com/webservices/finding/latest/findingservice.wsdl");
//1st argument service URI, refer to wsdl document above
//2nd argument is service name, refer to wsdl document above
QName qname = new QName("http://www.ebay.com/marketplace/search/v1/services", "FindingService");
Service service = Service.create(url, qname);
face hello = service.getPort(face.class);
System.out.println(hello.getHelloWorldAsString("test"));
}
}
the second class is:
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
#WebService
#SOAPBinding(style = Style.RPC)
public interface face{
#WebMethod String getHelloWorldAsString(String name);
}
the third class is:
import javax.jws.WebService;
//Service Implementation
#WebService(endpointInterface = "face")
public class endp implements face{
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
I'd be thankful if I can get some guidance. Is it possible to access services like that or do I have to use the ebay API (with keys etc..) ?

WSDL is just a contract between the client and the server.
The best solution is not parse the WSDL in runtime and try to call the actions. You probably want to call these actions to do something useful. Ex: find all user Ebay orders.
Each of these actions can have complex inputs and outputs. The best solution to work with SOAP webservices in Java is usually generate code based in the WSDL, so you will have a full working client without too much work.
I can recommend these APIs:
JAX-WS:
http://www.mkyong.com/webservices/jax-ws/jax-ws-wsimport-tool-example/
Spring-WS:
https://spring.io/guides/gs/consuming-web-service/
Axis 2:
https://axis.apache.org/axis2/java/core/docs/userguide-creatingclients.html

Related

How to identify whether web service is REST or SOAP?

I am learning spring MVC and have wrote following code. I read some articles about SOAP and REST but in the beginner level controller code I have written I am not able to distinguish whether SOAP or REST is used. My controller code is as follows:
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.Customer;
#Controller
public class SelectController {
#RequestMapping("/")
public String display(){
System.out.println("Inside controller");
return "demo";
}
#RequestMapping("/index")
public String indexpage(HttpServletRequest req, Model m){
String name = req.getParameter("name");
String pass = req.getParameter("pass");
String random = req.getParameter("abc");
System.out.println("Index page"+name+pass+random);
Customer cust = new Customer();
cust.setUsername(name);
cust.setPassword(pass);
System.out.println("Index page"+name+pass);
m.addAttribute("customer", cust);
return "hello";
}
The Controller that you have written is
neither REST nor SOAP.
Its a MVC Controller.
In your case, your returning "hello" string at the end of controller method, which in-turn gets translated/mapped to a page(hello.jsp or hello.html based on the configuration) and returns the same. So, at the end, what you get is Page rendered in a beautiful way with all the necessary Stylings and scripts applied.
In contrast, REST and SOAP doesn't work in that way. Its main purpose is for transferring the data alone(Yet you can send HTML page also)
Writing a REST Service is almost similar to what you have currently and is fairly easy. If you use Springboot then all you have to do is just replace the #Controller annotation with #RestController and return Customer object directly. In REST Controller you wont have HttpServletRequest & Model arguments.
But writing a SOAP service is another topic which may seem entirely different.
There are tons of examples and tutorials scattered around the web, which you can find easily on these topics.
References :
Controller vs RestController in Spring
Difference between Controller & RestController in Spring
SOAP vs REST
Hope this gives some high level idea of what those three are.

How do I get the parameters from a webook notification into my AWS Lambda function?

I am a bit confused as to how I am meant to hook up my Lambda functions via API Gateway to Braintree's Webhooks. I know webhooks invoke my lambda functions via api gateway via and endpoint URL but I am unsure how to set up my lambda function to handle this properly and use the values that webhooks will pass as parameters when invoking the function. I have the following right now:
package com.amazonaws.lambda.submerchantapproved;
import java.util.HashMap;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Environment;
import com.braintreegateway.WebhookNotification;
import com.braintreegateway.WebhookNotification.Kind;
public class SubmerchantApproved implements RequestHandler<Object, String> {
public String handleRequest(Object request, Context context) {
BraintreeGateway gateway = new BraintreeGateway(
Environment.SANDBOX,
"MyValue",
"MyValue",
"MyValue"
);
WebhookNotification webhookNotification = gateway.webhookNotification().parse(
request.queryParams("bt_signature"),
request.queryParams("bt_payload")
);
String woofer = "";
return woofer;
}
}
This is not working or correct though. How exactly am I meant to get these bt_signature and by_payload values into my lambda function?? The webhooks pass the data in via a http-POST request which is relevant.
Well, your Object request is exactly where those request parameters should be.
There are two main scenarios for Java Lambdas:
You can configure API Gateway to in proxy mode and parse input stream in you Java code.
AWS guys were very kind to write this ready-to-use example for you: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-java
You can use traditional API Gateway mapping, but then you'll have to implement a request class that will implement those parameters like bt_signature and by_payload. Again, a great template/example is available from AWS: http://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html

Java SOAP client using SOAP UI library and WSDL file

I'm trying to build a Java based SOAP client which imports a WSDL file and sends a request to the end point specified in the WSDL. I'm currently using the SOAP UI library and while it can compile, it is connecting to the wrong endpoint. In addition, I'm not sure where/how I define the authentication credentials (user/pass).
I am using a base code found on this site but this doesn't include authentication. It is also getting the endpoint from the wrong attribute when I run the code. Please help!
package com.bbog.soap;
import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
import com.eviware.soapui.model.iface.Operation;
public class WsdlAnalyzer {
public static void main(String[] args) throws Exception {
WsdlProject project = new WsdlProject();
WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "file:/home/asarkar/Documents/EthocaAlerts-Sandbox.wsdl");
WsdlInterface wsdl = wsdls[0];
for (Operation operation : wsdl.getOperationList()) {
WsdlOperation op = (WsdlOperation) operation;
System.out.println("OP:"+op.getName());
System.out.println(op.createRequest(true));
System.out.println("Response:");
System.out.println(op.createResponse(true));
}
}
}

How can i create a java web service that supports multiple client requests using only java SE?

I have the task of creating a web service that takes requests from multiple clients. I am new to web services so i used this tutorial:
http://www.java-forums.org/blogs/web-service/1145-how-create-java-web-service.html
This is exactly what i am looking for. A web service with no java ee.
It is preferable that is stick with java se, it's a policy that is prefered to be kept.
Now i would like to go one step further and implement the service so that it processes requests from multiple clients that operate on a shared resource.
Ideally i would like something like this:
Client client = new Client();
client.processRequest(string);
And the web service will process the requests in the order they arrive. The requests will come in as an request is processed so it will be kept in a stack.
The problem is i just on't know how to send the response back to the specific client. The response will be a string. The only thing i came up with, at least in principle, is to send a object that remembers where it came from but that just seems the web services job.
I have searched the internet but did not find a solution.
If possible using only SE please help.
If you think it is not possible without EE you can say so, but i would very much like an answer using only SE
I think what you are trying to implement is an Asynchronous Webservice. The following link tells you how to implement it in Java SE.
http://java.dzone.com/articles/asynchronous-java-se-web
You can do this using the Endpoint.publish methods in Java SE. First, you create a simple "endpoint interface":
package com.example;
import javax.jws.WebService;
#WebService
public interface ExampleService {
String getDateTime();
}
Then you specify that interface in an implementation class. As you can see, both classes must be annotated with #WebService.
package com.example;
import java.io.IOException;
import java.net.URL;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Service;
import javax.xml.namespace.QName;
#WebService(endpointInterface = "com.example.ExampleService",
serviceName = "ExampleService",
portName = "timeService",
targetNamespace = "http://example.com/time")
public class ExampleServiceImpl
implements ExampleService {
public String getDateTime() {
return String.format("%tc", System.currentTimeMillis());
}
public static void main(String[] args)
throws IOException {
// Create server
Endpoint endpoint =
Endpoint.publish("http://localhost:8080/example",
new ExampleServiceImpl());
URL wsdl = new URL("http://localhost:8080/example?wsdl");
// Create client
Service service = Service.create(wsdl,
new QName("http://example.com/time", "ExampleService"));
ExampleService e = service.getPort(ExampleService.class);
// Test it out
System.out.println(e.getDateTime());
endpoint.stop();
}
}
By default, JAX-WS will treat all public methods of an endpoint interface as web methods (since that is commonly what developers will want). You can have more control by placing the #WebMethod annotation on only the methods you want exposed as web services.
See the JAX-WS specification for all the details.

Web services cross-languages parameter types

I am creating a web service. I want to know how do I declare parameter type and use it
as Java type is different for eg. date. I have written client to consume web services in Java which is working fine, but I want to know whether I can consume same web services using client written in some other language. I am giving you a code example of my web service:
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;
#WebService
public class WiseQuoteServer {
#SOAPBinding(style = Style.RPC)
public String getQuote(String category) {
if (category.equals("fun")) {
return "5 is a sufficient approximation of infinity.";
}
if (category.equals("work")) {
return "Remember to enjoy life, even during difficult situatons.";
} else {
return "Becoming a master is relatively easily. Do something well and then continue to do it for the next 20 years";
}
}
public static void main(String[] args) {
WiseQuoteServer server = new WiseQuoteServer();
Endpoint endpoint = Endpoint.publish(
"http://localhost:9191/wisequotes", server);
}
}
Jigar is correct. JAX-WS uses JAXB to convert parameters into XML. JAXB has an extensive set of annotations that you can use to customize how the data is turned into XML. Since the data is sent as XML almost any language can read/write to the service. Furthermore, most languages have some kind of SOAP library available.

Categories