Converting methods to web services in JAVA - java

I am currently reworking a legacy JAVA data driven web application to a MVC model. While reworking, I realized that almost all servlets call a getID() method and almost 10 other methods. I am thinking of converting these method calls to a service. This would be my first web service development. I use Eclipse and weblogic. While I am reading up online, can someone give me a brief outline of how this can be achieved to get a head start? Another question is, do these services get invoked from a business class or a servlet or a jsp or from all of these? Thanks.

Web service can mean XML-RCP, SOAP, or REST. To me, web service means "calling a networked component via HTTP." If you agree, you can simply refactor those methods into another servlet and call it a service.
Don't use XML request/response unless you need to.
I would not invoke services from business classes. Servlets and JSPs are firmly in the web tier, so I can both calling a web service (e.g. AJAX calls from a page).
But my preference would be to have a true service layer, with services calling services. The service layer is more reusable than it'll be if you embed that stuff in the web tier. What happens to all that code if you go with a mobile UI? The web tier should validate and bind input parameters, call services to fulfill the use case, and package up the response for display. Let the services do the heavy lifting.

Related

Expose Adobe AEM bundles as restful webservices

I've created an Adobe aem bundle. I want to expose it as a restful web-service.How can I do it. I see there are explanation for consuming a third-party rest service but not enough information as to how to expose your own AEM bundle as a rest service.
Exposing a proper REST API is quite a challenge of its own. I'm not going to dwell on it here because there's a lot of excellent material on the web. Just google for REST and HATEOAS. The book RESTful Web APIs by Leonard Richardson and Michael Amundsen also describes the subject very nicely.
That said, I'm not sure what you mean about exposing an AEM bundle as a REST service but it's probably going to take quite a lot of design work.
An OSGi bundle (I assume that's what you meant by AEM bundle (sic!) ) can potentially expose a number of OSGi services, each with a separate set of available methods.
While you could technically draw a parallel between an OSGi service (with its own API that's basically a set of Java methods to be called by other components in the OSGi environment) and a RESTful web service (with its hypermedia-driven API available over HTTP), the design constraints for both types of services are completely different. You can't just expose an OSGi service using a RESTful web service.
What you need to do is to design a RESTful Web API and back it up with the OSGi bundle that you have.
One way this could be done is by creating a number of Sling Servlets. These servlets are themselves OSGi components and, therefore, can ingest OSGi services that your bundle already exposes.
I have no way of knowing what your API is supposed to do but if it's about storing data in the Content Repository, you should keep in mind that Sling itself is built around the principles of REST. What you want to achieve may well be doable using the OOTB servlets and appropriately composed forms (hypermedia controls).
As AEM is built on top of REST architectural concept it exposes Restful endpoints via Servlets. You can use the default SlingServlets like GET/POST methods or write your own Servlets by extending the SlingAllMethodsServlet (i.e. Sling Servlet that accepts GETs or POSTs)
Other clients that are able to perform REST requests can sent REST requests to your AEM (calling your AEM Servlet) by performing GET or POSTs.
For an example JSON representation of the OOTB content by using the default sling GET Servlet can be seen by below urls which renders you the content in JSON format with child levels(depth content) based on the selector
http://localhost:4502/cf#/content/geometrixx-outdoors/en/men/coats.json
http://localhost:4502/content/geometrixx-outdoors/en/men/coats.1.json
from the client end there are multiple ways of calling these Servlets like by AJAX, JAVA SWING applications, HTTP FORM Post, etc,.
Some examples are shown in below articles.
AJAX CALL TO SERVLET, USING .net call to AEM, Using AEM POST CALL
~Hope it helps
if you create a class in you core project under servlets package, you can define a restful service using annotation:
#SlingServlet(paths = "/bin/pagesutils/importservlet", methods = "POST")
declaring also the method and the path.
Remember to allow the path of the servlet in your dispatcher configuration.

Use utility class in web application or web service?

I know how to make web application in technologies like jsp/servlets and applications servers.
Suppose I make some utility method for particular application say ‘A’ and its working fine no any other web or desktop application in need of this method.
Same thing I can provide such utility method via web service like Rest/Soap.
So why I need web service in this scenario, I can imagine if such service is useful for other application and we provide it as web service then its fine.
Can anyone give me some answer which clear my doubt?
Thanks
Based on Your Comment, i would like to clarify.
Web service is a concept in which you expose a utility or functionality to the world.
Any one in the world can access the same by first establishing ground rules as in SOAP via WSDL.
The Services can serve any number of requests from any application provided it sends the proper input request needed by your service.
In case you have built an application A with a utility functionality X,
Then application B,C,D and so on can access the Web Service.
All it needs is:
the URL for the Service which is exposed through network to the consuming application
The requested parameter format
and the Proper Response format.
Once this is setup, any application, not just in java, can access the service so even .Net applications or PL/SQL applications can access the Web Service Utility.

Client architecture for calling Spring based web service

I have written a SOAP based web service which runs fine on a Tomcat server. The Web Service service itself is a Spring MVC based web service that runs on the Tomcat application server.
Now i need to write a Thick client which will be a standalone Java app that will use the services of the web service. I think i am correct in that the client only needs to know about the service details (i.e. operations) and nothing else.
What i am not sure of is the architecture and environment i should use for the client. The client application will be based on Swing but is it possible to use Spring with Swing together?
On the web service i have the following setup
view --> Service --> Model
The client application is basically a configuration tool. It uses the web service to configure user accounts. This means that the client application does not actually write anything to any database. It just uses the services of the web service to make changes to 'user account' and probably view list of accounts.
My question really is
- Is an MVC design suitable for such a use case
- Usually Spring is used for web based applications. Is there any benefit in using Spring with the Swing based client?
- Are there any alternative or better solutions/design/architecture that would achieve the same?
An example showing Spring used in conjunction with a Swing application would be very usefull.
Thanks in advance.
Spring MVC is not appropriate for a Swing-based client. Use the core Spring framework and a JAX-RS implementation like Jersey to provide simple REST web services in tomcat. Jersey also provides a corresponding client API that you can use within your Swing application to invoke the REST services.
If you have decided upon Swing as your platform, there are two options you can look at:
(1) Net Beans Rich Client Platform
http://netbeans.org/kb/trails/platform.html
(2) You can roll up your sleeves and write your own app using a low level yet extremely flexible framework called Swixml
http://www.swixml.org/
Give Swixml a good try before you try others, it may surprise you.
You can implement Swing-based thin client application with Spring Integration backend serving as a integration tier. It can expose gateways accepting simple Java types or DTOs. Your Swing presenters / controllers interacts with these components in order to call remote webservices.

Generate SOAP Web Service client forms (Java)

I have a Web Service accessible via SOAP. Let's assume it provides a method with the signature
sayHello(String name)
Of course, I have the WSDL describing the Web Service.
What I want to do now is to generate a client web application (war archive) with a GUI that provides a form to enter the parameter for the Web Service method. In case of the example, the form must just allow to enter the value for the "name" parameter. Then, a SOAP message must be assembled and sent to the WS.
Is there any way or any framework to generate such a webapp automatically!? The actual kind of the resulting webapp is not important, it may be a GWT webapp, JSF, plain Servlet with JSP or whatever. Even a plain HTML/JavaScript client app would be OK.
I mean, there are tools to generate CRUD forms out of data models, so there must be tools to create forms for Web Services, too...
I've been googling around for a long time, but the only thing I've found is a feature of Eclipse: http://www.eclipse.org/webtools/jst/components/ws/M3/tutorials/WebServiceClient.html . Basically, this does what I want, but I'm looking for a more ..hm.. elegant way to do this ;-)
Thanks in advance,
Frank
What you are looking for is (I think) similar to what this site seems to do: link soaptest
As far as I know there is no framework that supports this out of the box.
All frameworks support the automatic generation of client stubs and artifacts and application developers use that to implement their functionality.
In your case create the HTML interface your self to test the web service.
Only .NET web services provide similar tool for testing link text
If I remember correctly, Netbeans provides simple web pages to test the functionality of web services methods (methods offered by the server). You can find a decent amount of guides showing you how to create wevservice clients on youtube. It is a relatively straight forward process.
If it is just for testing the web service: go for soapUI. It's a standalone application and (to my opinion) a must for every SOAP engineer.

Translating SOAP Web services calls into JMX API

I have SOAP web services for Application 1 available to me. And, I'd like to implement a Java application that translate Application 1's Web Services calls to Application 2's JMX API. So App 1 can manage a bunch of operations through App 2.
If I understand the problem correctly, I want to build a SOAP/JMX Proxy (Remote Proxy design pattern) as my translation layer. This means it will receive SOAP requests and translate them to JMX, forwarding the translated request and then do the inverse to return the response.
Do you have suggestions on this approach and if there is another way to proceed? Any pointers/corrections are highly appreciated.
Epitaph, this looks like a repost of your same question Ideas on how to approach a project involving Integration of APIs. In essence, it is the proxy pattern. Your web services are just calling another object via the JMX API.

Categories

Resources