How to connect webservice to web interface? - java

Assume we are designing a Spring webservice. We need to build also a web interface for it. As i understand, browser needs an HTML document to display it to user.
Can HTML be created on server and be sent as response to client's browser or only xml docs can be a response? Any details would be helpful.
If we need to process an XML response, build HTML and display it in browser, what is the most convenient way in Java context?

Best way you can make Spring boot restful web service and get json output from it and display it with Angular2. or you can use Spring Mvc and create web template with jsp pages and you can display it on browser.
here is the best video for follow spring boot

1 - You can have static HTML on the server that is served as user requests your webpage.
2 - Add some javascript functions on the HTML pages to request your Spring java aplication endpoints and return response as json (or xml). For this purpouse Angular could be a good choice as stated above.

Related

Rest Webservice vs Web application?

I have recently gone through rest web services(mainly spring). But I did not find much difference between rest based web service and web application.
In rest based web service, we have #RestControllerand in web application we have #Controller. The one difference from dev perspective I know is in rest that we have more
verbs like PUT, DELETE etc. but in web app we mainly use POST/GET . That is from receiver side. Even sender will just sends the http request for rest like sent in web application
Both maps the incoming url with method , mentions return format etc.
Yes there will be difference in authentication as in web application it will be form based authentication but web service it will be different like header based or something else.
So is there any major difference in protocol/sender/receiver or any perspective ?
From spring-frameworkrestcontroller-vs-controller
The key difference between a traditional Spring MVC controller and the RESTful web service controller is the way the HTTP response body is created. While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML
Rest of the things are more or less same
Web services are typically from application to application or machine to machine to exchange data it and the raw data usually is not human or browser friendly, ( encoded in json or xml or other agreed formats). The encoded data maybe later transformed or wrapped into pretty web pages as an output to view object data to be browser human-friendly for viewing.

Separate frontend and backend in Java - Spring MVC Framework

I'm new at Spring MVC framework i want to learn how to fully separate frontend(html,js etc.) and backend(java).
I'm going to use RESTfull services, play with JSONs.
I think i'm going to build Single Page Application.
Most of tutorials shown in jsp pages which i dont like.
I saw my friends company project(Using Spring MVC) they used Embedded Jetty server and in server configuration they assigned two different paths for frontend and backend paths.
I saw frontend codes there was only html javascripts etc. on the backend side the approach was the same.(Fully Separated !!!)
My question is:'How they pass requests from frontend to backend and get results from backend and update frontend'.
Also they were using Maven backend and frontend defined as modules in the root.
Could you share tutorials so i can understand playing with codes ?
'How they pass requests from frontend to backend and get results from
backend and update frontend'
They probably use HTTP[S] as the transport and JSON as the data representation format. Browsers support AJAX which allows you to make HTTP connections without reloading the page.
Could you share tutorials so i can understand playing with codes ?
No, that's not what this site is for.
Comments:
JSP is still very useful for generating HTML on the server. This is pretty close to necessary if you want Google to crawl your site.
Check out Spring Data REST for a framework for quick REST APIs.
Also check out ExtJS or Dojo for good Single Page App frameworks.

Frontend framework to consume RESTful backend

I've build a RESTful backend in Spring + Hibernate framework. Now, i'm starting with frontend in Spring MVC. So, my jsp pages make an AJAX call to the frontend URL which inturn calls the backend URL to get the data.
Is this a proper way of implementation? Or should i re-think on the design? How about replacing frontend Spring MVC with Angular.js or any other framework?
So, my jsp pages make an AJAX call to the frontend URL which inturn calls the backend URL to get the data.
No. The JSPs are your frontend. JSPs are one way to generate and deliver HTML to the client. When it is successfully delivered to the client, the HTML is rendered and the JS is executed.
Within the JS-code you are able to make calls to the backend to get your data.
Is this a proper way of implementation?
Yes.
How about replacing frontend Spring MVC with Angular.js or any other framework?
You are free to do that as you like.
Since recommending one or the other framework would be based on my subjective opinion, I won't say anything but: Angular is en vogue. So: yes, it is one possible solution.
And since Angular itself is only a JS-framework you could combine it without further ado with JSPs.

Redirect html page from rest web service

I am new to all html and rest web service.
I have use html form in index.jsp file which accepts the username and the password.
This form call rest web service using action attribute.
Web service authenticate the user from the username and the password provided.
Now depending upon the authentication done by web service, I have to send various pages to client's browser again.
Like for manager, employee, admin, there will different pages for each one of these. Depending on the authentication, designation is fetched from the database in web service.
I have tried with doing Response.TemporaryRedirect(uri); and Response.seeOther(uri);
But it didn't work for me.
Is there any other way to redirect page from rest web service?
Thank you.
Solved, I need to write the code for redirection using Response.TemporaryRedirect(uri)in the finnaly block. It works for me.
As i mentioned earlier and as you said you have HTML and your REST service at the same location, it would be great to look at Servlet Filters... They primarily are called whenever your application REST URLs are accessed
If you are using JSP, and you have REST at the back, you can still use Servlet Filters because you are talking about a web application.

How to make data from a database accessible from other web applications

I have a database from which I want to expose data.
Ideally I would like to be able to just add a URL into some other web page and that URL would then call the correct datum using the web app I use to interact with the database.
Would a web service be the best option?
Looks to me like a perfect job for ODATA:
The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.
See it action (showing query results in a browser is just one way to use ODATA).
A URL-based solution as you describe would only work if:
a) the web app framework you use can resolve the URL automatically as it parses and sends the HTML to the browser, or
b) the browser resolves the URL (e.g. the IMG element)
If the web app framework you use can resolve the URL (or if you can extend it so that it does), then you still need something that listens at that URL and retrieve the correct element from the database.
The approach here depends on whether you are doing Ajax style web pages or simple HTML, where each UI update refreshes the whole page.
The latter, a traditional page by page web site, it probably the simplest thing. For this explore JSP technologies. The idea is that you write what looks like an HTML page, but embed in it references to Java objects (or even Java code). In this case you should read up on simple frameworks such as Struts. The broad-brish idea is that you get this sequence of processing
Request arrives from Broswer, interpret it to figure out what the user wants to see
Some Java code talks to the Database gets data puts it in a Java Object
A JSP is chosen, that JSP picks items from the Java Object we just prepared
The JSP renders HTML which is sent to the Browser
In the case of Ajax, JavaScript in the Browser decides to display some data and calls a service to get it. So here, yes a "Web Service" of some kind is needed. Usually we use REST services, which return a payload in JSON format, effectively the data is transferred as JavaScript.
There are plenty of libraries for creating RESTful Web Services, for example Apache Wink.

Categories