RESTful webservices with Struts 2 - java

We are evaluating one design for educational activity. We would like to create complete backend (which handles business logic) in form of RESTful web services.
These services can be used by various apps. Is it good idea to call these web services from Struts 2 framework? I read couple of docs, and people discourage it.
We would love to go with Struts 2 as team is quite strong on it. But if its bad approach, we may consider for other options.

If you want to leverage development on presentation layer between client-side and server-side (this is where Struts2) then you can utilize web services using a web services client API. At this point these web services can be used as data resources.
If you want to stay only on client-side then you don't need any server-side framework for the frontend development.
Note that The introduction to Struts2 and RESTful applications you have posted in comment introduces an alternative way instead of web services for creating backend in a form of RESTful API using Struts2 framework.
Using Struts2 for the REST instead of web services could be a solution for RAD (Rapid Application Development). Because web services are very complicated technology that used in upper high level architectures and using some alternative resource APIs might be cost effective.

Related

RESTful and J2EE

I have developed a web application with Java EE, which connects to a DB. The app is deployed on Tomcat 8. The app is divided in three layers: db layer, business layer, and presentation layer.
Now I need to develop a RESTful API that will use the business layer and will provide most of the functions that the presentation layer provides. Clients will have two options to choose from: open a browser, connect to the APP and use it or consume the RESTful web services from their own software.
My question is: should I deploy the RESTful API on the same server where the APP is deployed or separately? What are your suggestions?
And, what kind of authentication would you suggest for the REST web services?
Thanks!
It is a rather broad question and the short answer is it depends.
Functionnally, you have three parts here:
the presentation layer
the API interface
the back office: business and db layers behind the 2 first ones
Common technical architectures are:
one app for the API and business and db layers, one app for the web layer using the API
everything (API, Web and business) on the same application.
The former offer a better separation and can be interesting for heavy loaded applications, or if you plan to move to a javascript interface (AngularJS ofr example), the latter will be simpler to implement.
For the authentication, it is simpler to pass the credentials along with each request for an API, but you should considere managing it outside the application itself through filters and/or AOP concepts. Spring Security is an example of how this is possible and gives a very loose coupling between the business code and the authentication and authorization ones. You can then choose and change your authentication methods with little impact on the core of the application.

How to build configurable facade over set of RESTful services

It's time to ask question on StackOverflow because I did not find a good one in Google.
We have a legacy systems(2 in my case) that provide some functionality via RESTful web-services.
Now we are building a Java EE system that will consume RESTful services from that legacy systems.
Questiuon: how to build convenient facade API over mentioned RESTful web-services to use them easily in our Java EE app?
Are there some frameworks to easily consume a set of REST web-services and it can be XML-configured?
There is a number of options
The most "low level" - HTTP Client (http://hc.apache.org/httpclient-3.x/)
Something more abstract - RestEasy ClientRequest/ClientRespons (http://docs.jboss.org/resteasy/docs/1.2.GA/javadocs/org/jboss/resteasy/client/ClientRequest.html) there is a number of tutorials how to work with it
You might like it most due to XML config - Spring Framework RestTemplate support (see, for instance http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html), there is a number of tutorials in the web too.

For performance, Spring webservices or pure restful jersey even if i use MVC?

im using spring MVC and webflow to create a game server and serve some web pages to the users. Thing is, the javascript game will also make multiple ajax calls to restful services on the same server for some game logic. While the web page serving performance is not critical, the restful service calls need to be as efficient as possible (efficient as in response time).
For performance of the services, would it be better to use pure JAX-RS (jersey) web service calls without the spring ws overhead (since i understand the spring layer could affect performances negatively) or would it be the same if i used the spring webservices framework instead and maintain integration with the rest of the spring family?
thanks!
There aren't many clear benchmarks out there, but take a look here:
http://www.techempower.com/benchmarks/
It clearly shows the overhead of using Spring. Compared to Servlets that serve JSON manually Spring is "slower". Personally, I don't think that Spring cuts development time, unless you are very much familiar with it. Creating a simple servlet that will act as a REST API is very simple. Take a look at the servlet code from the benchmark:
Servlet benchmark
I don't think Spring per se will affect performance negatively. Where did you hear that?
Spring web services are "contract first" SOAP services. If you're saying that you don't want to pay the overhead of SOAP, XML, marshalling and unmarshalling, then you have a valid point. That's true whether you use Spring to implement the services or not.
REST is HTTP, so it's a natural for AJAX calls.
I believe Spring 3.0 supports REST, so it's not a "Spring or not Spring" choice:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#d0e1188

what is spring web services used for

I am building websites in spring MVC. as there are many spring projects , i wanted know what will be scenario when spring web Services is used for.
i mean what can be done with that. Do i really need it for ecommerce site
Your question is really about "web services". For a layman's explanation of what it is all about, read this Wikipedia article.
There are a number of related acronyms that get bandied about:
SOAP (Simple Object Access Protocol) is a protocol for sending requests to a service and getting back a response. The messages are encoded in XML, and send over HTTP.
WSDL (Web Services Description Language) is a service description language for SOAP-based services. It does things like specify what is in the messages, and how the messages are bound to services.
SOA (Service Oriented Architecture) is essentially a system architecture in which the system consists of lots of SOAP services (and others) described using WSDL.
(The proponents of SOA talk about "design principles", but in my cynical view is that this is just a repackaging / recasting of stuff that people have been doing for 20+ years under other names.)
Do i really need it for ecommerce site
Ask your customers. Ask the people whose systems your system will be interfacing with. Ask the vendors whose components you intend to embed in your system.
If you have to use WS then what extra facility it will give
If you have to use WS (e.g. because your site needs to talk to other services that require WS), then you have to. That is sufficient justification.
If you don't have to use WS, then you need to balance the advantages of using WSDL + SOAP against the advantages of some other approach to implementing your web APIs. An SOA expert will probably say use SOA; an AJAX expert will probably say otherwise. And there are other remote procedure call technologies around ... if you really need that kind of thing.
WSDL + SOAP certainly does have some advantages; e.g.
machine readable specifications for your web APIs,
possibility of validation of messages against XML schemas,
an ecosystem of existing WSDL services,
adoption in some sectors of IT.
But it has downsides also; e.g.
WSDL + SOAP have a significant learning curve compared to some alternatives,
XML is a heavy-weight encoding scheme - relatively expensive to parse,
SOAP only pretends to be type-safe (compared with say CORBA / IIOP),
SOAP is not usually used* in browser-based clients; JSON or plain XML are commonly used for AJAX apps,
many people think SOA is over-hyped and steer clear.
* However, it can be used; read these IBM DeveloperWorks Articles.
My advice (FWIW) if your system is primarily a website, doesn't need to talk to SOAP services, and doesn't need to provide a SOAP service API for others .... don't bother. You can always add SOAP service APIs later if you need to.
Spring Web Services is a product of the Spring community focused on creating document-driven Web services. Spring Web Services aims to facilitate contract-first SOAP service development, allowing for the creation of flexible web services using one of the many ways to manipulate XML payloads.
More here : Spring Web Services
How to use Spring Web Service : Spring WS
If you don't know what Web Services are, you probably don't need them. Web Services are a way (speaking generally here) for one web application to talk to another. You can formulate a request as an xml document and send that xml to a web service, which is listening on a specific URL and port. The Web Service can then perform some action based on this document, and respond back with an XML document. It's commonly used as a means for integration between applications.
If you're building a simple e-commerce site, which does not integrate with other live web products, then likely you don't need Web Services. Spring Web Services is simply an API/library to make reading/writing web services easy.

Simple RIA backend

I'm creating a prototype for a java web application.
Frontend is a Swing-based java applet.
Backend should be a type of web-service, that is called by applet.
Backend should run inside a servlet container and should have its own security (username/password) database. I know, that Tomcat has its own user database (realm), but the app should have own. Web-services, in turn, carrying out app logic and database access (via Hibernate).
I'm a newbie for a web development and I'm getting lost in a huge amount of the java web frameworks. Even just reading 'introduction' and 'getting started' documents takes a lot of time.
So I need an advice which framework(s) are suitable for the task and not very complex for a quick start.
Thank you
I would avoid any web framework in such case. Most frameworks are designed so that it is easier to connect bussines logic (backend) with WWW user interface. In your case you don't need web GUI - you have an applet, so web framework like Stripes, Struts, etc. would not help to much.
I think you can use servlet or several servlets as a connector between backend and you applet. Servlets are simple, easy to learn.
If you want to have some abstraction layer with additional services, like security, for instance, you can consider Spring Framework, but it has its own learning curve.
Spring (http://www.springsource.org/) seems like a good choice to handle DB access, and server side logic. Spring-security can be used to integrate security (it takes some time to get started, but it works very well). SpringMVC can be used to output simple XML documents, or if you need more complex remoting capabilities, SpringRemoting is a good solution.
If you want to go the full WebService (with a capital W and a capital S) Spinr-WS can be useful.

Categories