Java MVC Communication - java

So I am pretty new to Spring and the whole MVC structure. Currently we work with tomcat server and another layer that we call a "service" layer. I am confused on how the tomcat layer communicates with the service layer.
So for example: Lets say I am validating passwords for users. So the idea would be that it would first be validated on the UI side aka the tomcat side via javascript and after that these values would pass over to the service layer where the most of the back-end logic lies and the values would be validated by Java. So my question is how exactly are these values passed from the UI end to the back-end layer?

This would be good start for you: Spring: Serving Web Content with Spring MVC
Once you have a backend service created, your frontend will call the service as a REST endpoint, values will be passed as GET or POST request with the request to this backend service

Related

MVC - Spring Boot and Angular - What is the View in Spring?

I created a web application using Spring Boot for Backend and Angular 8 for Frontend with the help of the JHipster framework. The frontend communicates with the backend using a REST API.
I know that both Spring and Angular use of the MVC model. As such, in the case of Spring :
the database, JPA classes, and repositories are the Model
the REST Controllers are the Controller
My question is: what is the View part in the Spring application? My guess is that the entire Angular application takes the role of the view. Is that correct?
The view part in Spring was popular due to the Spring MVC concept where the view comprised of JSP (Dynamic) and HTML (Static) pages.
As you rightly said Spring and Angular both are MVC based frameworks, but now the current trend is moving towards Microservices architecture instead of Monololithic architecture.
Microservices architecture is language independent for its operation and communicates using REST services.
Currently, the focus is on separation of concerns and how quickly the code can be delivered.
1. Angular framework handles the UI part
2. Spring framework handles the backend operations (Business logics, interactions with the database and other servers.
In spring framework the MVC means
Model - The class that carries data
View -The view page (JSP/HTML)
Controller - The class that manages the operation
As you said on the post The database, JPA classes, and repositories are the Model these are nothing but the repository pattern which is integrated with the MVC pattern to communicate with the database.
To start Angular does not use MVC, Angular uses MVVM (model-view-view-model) or MVW (model-view-whatever)
Secondly your backend uses the n-layer pattern
Service Layer: Implement business logic
Data Access Objects: These are abstractions that your service layer will call to get / update the data it needs. This layer will generally either call a Database or some other system (eg: LDAP server, web service, or NoSql-type DB)
Controller Layer: Which will receive client requests.
In other words, you have a client-server architecture, with an n-layer pattern, where the angular (view layer) uses the MVVM pattern.
Observation:
Your Model you use may or may not come from your services. You may want to take the results your service gives you and manipulate them into a Model that's more specific to your medium (eg: a web page).
differentiate between database models (dao - data access object) and transfer model (dto - data transfer object)

Tickets booking (stateful) web service on Spring boot

I need some stateful web service ( let's say tickets booking in 5 steps 1) select ...5) pay ) and want to implement it using Spring (boot) framework
googling
spring web service
tons of RESTful web services examples could be found... but according to REST manifest and many articles/answers (i.e. Sticky Session for Rest API Calls)
REST client is made to call REST API and REST APIs should be stateless
Statefulness harms scalability
It's very easy to create a RESTful web service in spring boot due to great embedded and autoconfigured libs, and I dont wanna refuse of it.
So I see some ways to overcome this:
Create RESTful web service in Spring Boot and add to there session and store state on it, and add sticky session load balancer to maintain scalability ( it's more difficult then stateless approach but doable). Yes it will not be true RESTful service, but it will work
Create true stateless RESTful service, keep state at
temporary 'temp_transaction' database table which your code can consult to determine if a user is in the process of booking, say, a particular seat.
state maintained on client side not on server. So one of the way that i will suggest is that you can use cookies to store your state and temp data
Use some rich client side framework like angular or react ( im not good in it but believe there is the possibility to keep state presents in these frameworks) though I think anyway cookie used for these purpose so it's almost the same as 3)
Use Spring + SOAP. Soap can maintain state but I think this way is obsolete and modern newly created applications from scratch shouldn't use SOAP
Don't use spring framework for this project but use Front end framework ( mentioned at 4) + Node.js ( anyway it will be stateful)
So which approach better to choose?
Because your REST service
could crash and will then be restarted
you want to scale and have multiple instances of your service
you should keep the state in a database or a distributed cache like Redis.
You will have to pass a session key or a booking reference in every request. For example in the header.

Microserivce Using Spring Boot to Consume SOAP

I need to create a REST service that first consumes SOAP. What would the best way to go about this?
I would like to use Spring Boot to create a microservice, but I have a few questions for those with experience:
What other architectures or technologies should I look into using
(with spring boot)?
Is there a standard technology stack for this?
Are there pitfalls I should be aware of?
What other architectures or technologies should I look into using (with spring boot)?
My answer is if you just want to simply provide RESTful service
without Spring Cloud, then I think you can refer to following 2
tutorials on Spring official website to achieve this:
Building a RESTful Web Service
Consuming a SOAP web service
Is there a standard technology stack for this?
Currently, I will suggest you to use Spring Boot as your first choice. Because these are rich resources on the web and it does reduces much effort in development.
Are there pitfalls I should be aware of?
If you finally choose Spring Boot, please be familiar to its components, you can start from Guides to realize how it works. Or you may mix up Spring Boot with tradition Spring framework.
There are similar cases in our project and we did it with spring components. As far as i understood, you want to open a REST endpoint which most probably accepts json object and you want to make a soap web service request within that request then you want to return a response containing information from the soap response. To make a soap request, you can use spring web services - WebServiceTemplate. It will marshall your object to xml and make soap request for you. Of course you can use some other web service frameworks like apache cxf which may best fit for some special cases, but i would first try to use a framework which is from spring family while using Spring. You should set timeout values on webservicetemplate object to not wait too long if the external system is not working well or you have some problems with the network. Because it directly affects your systems performance. Also here, i suggest you to implement circuit breaker pattern to make your system more robust. You should always isolate your systems performance from other systems that you integrate and in this scenario you can do this by doing the things explained above.
As per my knowledge , you should use Spring boot application with Maven build.
In order to send a REST call to SOAP web service and get back a JSON Response ,you need to follow all of this steps in order :
Business Logic : Mapping the JSON fields such as headers, query-param , body variables to XML Request mandatory fields (either using pojo classes or object-mapper) Reason : Since the SOAP end point will only accept XML Request.
Service Broker Logic : Import "org.springframework.web.client.RestTemplate" and use
**ResponseEntity<String> responseEntity=RestTemplate.exchange(endPointURL, HttpMethod.GET/POST/PUT/DELETE, HttpEntity/headers, uriVariables)**
**endpointURL** -- SOAP End point URL ,that the REST service has to consume.
**HTTPMethod** -- Method Type such as GET ,PUT ,POST ,DELETE etc.
**HTTPEntity** -- Soap requires for mandatory sender/headers/{a}.Make sure that you set your header name and value as key-Valye pair in HTTP headers.
**uriVariables** -- (Object... urivariables) such as String.class ,Integer.class
You should also put the **connectTimeout** ,**isSSLDisabled**,**responseCached** elements while generating request to restTemplate.
responseEntity.getBody() is the XML response after un-marshalling.It can be extracted by using mapper.
XML_BaseResponse response=mapper.readValue(responseEntity.getBody(), XML_BaseResponse.class);
Business Logic : Extract necessary field from XML_BaseResponse and using setter's or getter's functions,set the mandatory fields in the response.
baseResponse.setName(xml_baseResponse.getPersonsName());
baseResponse.setAddress(xml_baseResponse.getAddress());
baseResponse.setCity(xml_baseResponse.getcityName());

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.

Connect grails front with java REST backend

I have Java backend project with REST implementation which is a warfile,
I also have Grails as frontend, i can run theese two separeted projects on their own using tomcat,
but how do i configure the grails part so that i can use the java backend RESTfull webbservice within it,
do i have to make a war file from the java backend and import it to the grails frontend ? make a jarfile ?
if so, how do i do that ? or are there any other way to to this to make it work.
REST provides a way to access a service without having to link them explicitly.
RESTful applications use HTTP requests to post data (create = HTTP-POST, update = HTTP-PUT, read = HTTP-GET, delete = HTTP-DELETE
If you run the Java backend using Tomcat, can you access it from a browser?
i.e.
http://localhost:8080/backendservice/resource/1
(this will be a GET request.)
You need to make the http requests from your application.
A lot depends on how much control you have over the 2 apps and how they have been designed.
First choice is to create a service class in your grails app that communicates with the other app using REST. You could use something like Spring's RestTemplate. The data is then passed to the Controller layer and into your front end JSP/GSP.
If the RESTful application has been designed in such a way that the RESTful layer can be separated from the business layer, then you could add a jar of the business layer to the grails app as a dependency. In this case you would just change the Service class to talk to the jar instead of the RESTful service.
Another option is to have the browser talk to the REST layer directly using JavaScript. This should work as they come from the same server.

Categories