How to implement REST web-services? - web-services

I am trying to learn Java Web-Service using REST architecture.
So far I have created a class but I don't know how to consume that service.
Here's my code:
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
#Path("/SericeCreation")
public class SericeCreation {
#GET
#Produces("text/plain")
public String display(){
return "hello";
}
}
Now, I want to call this display method.
Can anyone help me to understand how this can be achieved?

In a very simplified point of view, rest services are bound to Http calls, in your example the #GET annotation tells you that this method is accessed by making an HTTP GET call.
You can simply point your browser to the url generated for this service and you should see the "hello" response. The url is something like this http://<server>:<port>/<AppContext>/<rest-servlet-mapping>/SericeCreation where:
<server> is your server name. Typically localhost
<port> the default server port number. If you are usin Jboss 8080
<AppContext> typically the name of your applicaction (War file)
<rest-servlet-mapping> the RestEasy or Jersey frameworks servlet path
/SericeCreation this comes from the #Path annotation that is used
in your class.
Another way to intereact with your services is using a Rest Client like this one.
And of course, if you want to create a java client you can use the apache http client library, java.net.* package or others. Remember, rest services are accessed by making http calls.
I recommend you read this two tutorials to get a general idea of rest architecture.
Rest RefCard
Beginner's Guide
This great tutorial shows you a lot of examples using Jersey or RestEasy in order to implement Java Rest Services.

Related

C++ - Run on a HTTP Server as a Web Service

The question:
Is it possible to run a C++ library on a HTTP server and access it via a web service in another language?
In more detail:
A few years back I did some development work using C# where I connected and used a Web service, I made a connected and accessed the web service methods such as:
var myservice = service.getResults();
Which would return all of the results from the database.. I want to do something similar in C++ in that, I have a C++ library that handles all of the database connections and provides a series of methods in order to access. I now want to open this up to a web method.
The issue:
I don't know how I would go about running this on a HTTP server and accessing using methods that are in different languages? For example, a few of our clients use PHP whereas some use Python. I want to be able to access the methods from each of these languages, so, for example (pseudo code(
PHP:
// Connect to API (Web Service) e.g. http://localhost:8000
$apiClient = new WebService::API();
$results = $apiClient->getResults();
This would then call (in C++):
function getResults()
{
// Run DB query
}
The implementation of the methods I am not struggling with. It's more to do with how do I make this appear on a HTTP server and can I use something like SOAP in order to connect and provide the functionality? I just want hints or resources in order to do this.

Use CXF client api with Java web application

How to consume Web service suing Apache CXF client API.
I have generated the client Code using eclispe but I didn't found any document specifying how to use that generated code in my web application.
How to configure CXF? I am using tomcat to run my java web appliation.
How to use the generated code?
Do I need to add anyhting in my my web.xml?
I have downloaded CXF binaries from apache CXF website but don't know which libraries are needed. I am affraid i may end up adding all the jars.
I am using Tomcat 7, Java 1.6 and plane jsp/Servlet for my application
I am new to web services.
Thanks in advance
One sample code that may help.
URL wsdlurl=SOAPWebServiceTransport.class.getClassLoader().
getResource("my.wsdl");
// MyService will be one of the service class that you have generated (with different name ofcourse)and which must be extending Service class
//getOnlineServicePort will be a method (with different name ofcourse) in your service class which will give you the interface referrer using which you'll call the webservice method
OnlinePort service= new MyService(wsdlurl).getOnlineServicePort();
Client proxy = ClientProxy.getClient(service);
//configure security user name password as required
//Add interceptors if needed
//Now you can directly call the webservice methods using the service object
service.method(parameter)
you can also refer to some example one is here

Dropwizard - how would a client module look like?

I was wondering how the Dropwizard client module should be implemented.
Source of confusion:
Dropwizard recommends you to separate your project as such:
In general, we recommend you separate your projects into three Maven
modules: project-api, project-client, and project-service.
On the Client section, it shows that you can instantiate the httpClient provided by dropWizard within a run method.
#Override
public void run(ExampleConfiguration config,
Environment environment) {
final Client client = new JerseyClientBuilder().using(config.getJerseyClientConfiguration())
.using(environment)
.build();
environment.addResource(new ExternalServiceResource(client));
}
I thought that the client module would wrap the httpClient, and any other service can use the client module, without caring which httpClient it is using.
So
how would a client module look like
When would you instantiate an httpClient directly within a service's run method (as done in the snippet of code above)
Thanks!
How would a client module look like
This is heavily dependant on your project scope and structure. For example, in one of my projects which is heavily database dependant, the Client module (or Service class in DropWizard's terminology) contains my DAO instantiations as well as hibernate initializations and a bunch of other init stuff (SQS, etc). I also use the HTTP Client and the Service class is where I initialize it. Reason being is that the Service class is the entry point and this is where you end up instantiating your Resource classes, etc. So having the dependancies instantiated here allow me to pass them into my resources as constructor params. If you were using something like Guice, then the way to go would be different since you have access to injection, etc.
When would you instantiate an httpClient directly within a service's run method (as done in the snippet of code above)
The HttpClient shown in the doc and your question is used when your project requires a Http Client. For example, lets say your DW project or one of the resources you are writing requires you to make a HTTP call to a twitter API. This is where the Http Client comes into play. You can actually use any Http Client library you want, however using the ones provided by DW (Apache Http Client, Jersey Http Client) allows you to create a 'Managed' Http Client there by allowing DW to start up, shut down and clean up the HTTP Client when the service is shutdown. So things like thread pools, connection pools, etc are all cleaned up by DW when you use its managed HTTP Client. In addition, the reason why you create this HTTP Client inside the run method is because you are then able to get a reference to the Configuration object's instance which will allow you to control the HTTP Client's settings via DW's configuration system.
Hope this answers your questions

Send/Receive a SOAP request using SPRING JAVA

I am new to Spring web services. I am going to create an xml request, and send it as a SOAP request to a web service and receive the response.I read different documents but still confused as I could not find a working sample yet.
I know that I should use WebServiceTemplate and WebServiceMessageSender, SaajSoapmessageFactory (please let me know if I am wrong) but not sure how to use them.
Do I need WSDL? if yes why?
If you have any sample code please send me to get clear on it.
Thanks
If you want to send SOAP requests, you would like to be a SOAP client. Seems like you want to use spring-ws project. Check out their great documentation on the client side. The same documentation will guide you through the process of creating a server. There are plenty of examples and ready-made configuration snippets waiting for you.
Spring-WS is built on top of XML Schema description of your message, so you will need WSDL to generate e.g. JAXB models of your requests and responses.
AFAIK, for "web services" , the WSDL file is the machine blueprint of the "ports" as they are called However! ports in WSDL "means" java language(or any other programming language used with a routine or sub or procedure or function) method and has a specific naming scheme associate the .wsdl xml file(template of the service). Each WSDL port(language method) has specifications of return value and data specifications for how to feed it arguments and their type values.

Is there a way to connect to a soap webservice without using a web reference in C#?

is there some library out there that will accept the url of the soap wsdl file, and then generate me the proxy classes and what not at runtime? Web references are getting to be somewhat inconvenient for my particular situation.
Thanks.
Web Services Description Language Tool (Wsdl.exe) is what you want.
The following command creates a client proxy class in the C# language for an XML Web service located at the specified URL. The tool saves the client proxy class in the file myProxyClass.cs.
wsdl /out:myProxyClass.cs http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL