Is spring MVC purposed for web pages? - java

I want to build a server for my android application.
My application lets users register and allows each user to request a list of all users registered to my application, so my server will be mainly in charge of receiving data from a user, updating the database, and sending data back to user on request.
Since I've never built a server I looked into what would be the ideal way to achieve my goals and after some reading I've found that Spring would be the right way to go, But I also found that there are all kinds of springs.
Eventually I've narrowed my options down to Spring MVC and spring Boot,
I've read that spring boot is a good start but I also read that spring boot does all the configurations for you and I want to really know how stuff works so I fear that spring boot will do all the work for me , So I thought of maybe using spring MVC but I couldn't completely figure out if Spring MVC would be good to achieve my goals or if it's mainly used for building web pages
So what would be the best suitable spring to use ?

I would prefer Spring boot. It's not just about it doing all the configuration for you. It's about Spring saving you from writing a lot of boilerplate code (you still have to do a fair bit of configuration though). Plus, it will be easy to spin up the app and test it locally (you can even test it with local file based h2 database, meaning you don't need to install any database into your machine).
Adding Spring Data JPA dependency with Spring boot will take care of persistent layer as well. And if you want to write jsp or html pages then I would recommend having a look at this thymeleaf example.
Here's the sample CRUD application I have developed with Spring boot and here's my own blog about it.

Spring MVC stands for model,view,controller. View, in general is something which is returned after your business logic has been executed and mainly suggests webpages. Spring Boot would be the easiest way to set up your server for the application. However, if you want to know how things work you can go with the basic spring. Spring, too provided classes like JdbcTemplate to reduce your boilerplate code, however it forces you to configure things yourself.
You do not have the comfort of annotating a resource and watching as the magic happens. If you want to speed up setting up a server and make things less complex go for Spring Boot.

Related

Spring features and significance?

Spring empowers POJO based programming.
It offers support for MVC out of the box
wires up code in less time.
How does it support layered architecture?
provides an abstraction layer to simplify development process(why is this a benefit and how does spring handle this?)
How does spring do that?
I read a lot of articles talking about the advantages of spring. But none of them explain in theory i.e. in words not code, how spring does that?
for example, one of the advantages says it empowers POJO programming? we can do that with plain java too, why is that a benefit or what's the opposite of POJO?
Kindly request everyone to address those 3 questions. THanks
in short:
spring was created a long time ago. it was probably compared to ejb2 that needed more setup code. POJO vs EJB vs EJB 3 Also spring does support pojos for example as return value in MVC-controllers. pojos dont have external dependencies so your application stays portable (non-functional requirement of an application).
yes you can easily (my opinion) create web applications.
at its core spring provides a container in which bean definitions are stored. when a service is requested that needs a dependecy, spring can look up whether it has a bean definition for that dependency, construct an instance of that bean and inject it in that service. That way you dont need to manually instantiate your service.
for example in web applications spring has abstractions for controllers, services and repositories. typically an application provides controllers so that caller can use your api. controllers should call services that handle your business logic. and services can call repositories that persist your data. that way you have a layered flow:
controllers --> services --> repository
this has the advantage that when you change e.g. your repository you dont have to make changes to your controllers.
spring provides many abstractions for common problems out-of-the-box (e.g. spring security) and empowers convention-over-configuration. That way you can reduce boilerplate code. less code -> less probability for a developer to make a mistake.
e.g. spring-security-oauth2: you can just set some properties in a .properties file and spring autoconfigures beans that solve the validation of an oauth-token when a user requests one of your controllers.

Is there anyway to connect Java to React without having to use Spring?

So I have to build an offline project using MongoDB (with morphia), Java and React/Angular. I'm planning to work with react but all the solutions point to me using Spring Boot or Spring Data for this purpose. Is there no other framework that I could use to make this?
Its a very simple application where we need to list out some database entries and allow a user to make a request and change the entry values based on it.
I know Struts is also an option but remember that Im not actually going to deploy this online. This is like a show and tell to these lecturers on my own device.
NOTE I would have used Spring Data but as far as I know it doesnt have proper support for morphia? I could be wrong. Please let me know if it does
There are literally dozens of Java [micro]frameworks to chose from:
Vert.x
Javalin
Spark
Micronaut
Ratpack
Dropwizard
Ninja
Rapidoid
... and many more.
You can also try Play or Ktor or Grails. Or do some hardcore with Netty or Undertow.
Not to mention plain old servlets...
Vert.x in particular supports reactive MongoDB drivers: https://vertx.io/docs/vertx-mongo-client/java.

Spring web-based admin tool with CRUD

In PHP & Symfony world there is a tool called Sonata Admin https://sonata-project.org/ based on AdminLTE template, that is all-in-one admin tool with login, menu configuration, and what is most important - database tables HTML grid CRUD generators.
The tool saves you tons of efforts by avoiding you writing boilerplate code, moreover, there might be a scenario that you would write zero code.
We need the same but for Java and Spring stack. Any recommendations?
Take a look at JHipster project https://www.jhipster.tech/
With it you can create Spring Boot+Angular/React/Vue based applications. It provides some cool tools for generating CRUD entities by scaffolding.
https://www.javatpoint.com/spring-mvc-crud-example contains an example of how to create a Spring MVC CRUD there are plenty more examples of various spring project (This particular falls under Spring MVC).
This is not a ready-made CRUD, but a way how you could quickly implement one.
https://spring.io/guides/gs/crud-with-vaadin/ is also worth checking.
Although there are few project like spring batch admin (For batch administration), or Spring boot admin (Monitoring spring boot applications), which provides a UI, mainly spring project provides a simple programmatically way of plugging in what's needed.
It's a plug & play kind of framework which targets various aspects of an enterprise application. Chances of finding a ready-made UI app would be more on GitHub rather than a specific spring plugin.
Simple answer to your question is No spring does not provide any tool for that but spring gives you the functionalities that can reduce your work but not with zero coding.
example:
Using Spring Data JPA there is an Interface name curdRepository which
takes care of the curd Operations we dont need to implement the methods
for Curd Spring takes care of Curd operation.
Spring Security Provides the login configuration with the login form and logout mechanism.
You can use spring boot or just visit the https://start.spring.io/ it will initialize the spring project with the dependencies you need

AngularJS + Spring Project architecture

I am planning to start a project and I am looking for the best approach to make a RIA application using AngularJS.
Right know I am pretty sure of those technologies:
AngularJS (+ bootstrap CSS) for the client UI, logic and server
requests.
Spring for bootstrapping the server business logic.
Hibernate + MySQL for persistent data access
Jersey for the Restful web service API.
Spring Security for url and data protection over authentication.
The only piece I feel is not ok is that my application will not be the typical one page app, because it will be large and I want to break it into multiple one page apps, some protected and others public. To serve every index.html I want another technology like Spring MVC, making those small one page apps secure for this points, and also not allowing the access to some resources.
¿Do you think this is a good approach or you would change any of this technologies (like supressing jersey/Spring MVC redundant dependencies)?
i think that in general your aprroach is a good one, but maybe you could use the webapp generator yeoman with the JHispster, a java web app generator.
Or if you don't like the ideia you could add to your data access layer the Spring-Data-JPA, because you will avoid to write the boilerplate code.

Swing application with Hibernate and Spring

Can we use Spring as Controller and Hibernate as ORM tool for an application with User Interface SWING??
If yes how??
How will be its directory would appear. How all injections will takes place. For simplicity maintaining a environment for saving Text fields input into DATABASE using Hibernate.
Spring could not be used as Controller in desktop application (Spring MVC is a web framework), but it can be used for features such as autowiring.
However Spring and Swing does not play well together, my last attempt failed due to really inconvinient configuration and lots of overhead code.
Some time ago there was a project called Spring Rich Client, but seems to be abandoned.
Anyway - internet is full of examples of using Swing with Spring, but you have to know that this pair is "not that popular".
Spring: How to Create Decoupled Swing Components
SPRING HAND IN HAND WITH SWING APPLICATION FRAMEWORK
Using pair of Hibernate and Spring is not different than in web-application case.
No. You cannot use Spring as a controller. But you can use core Spring container for using in Dao and Service layer. Hibernate for using Object mapping with Database Table.

Categories