r/learnjava • u/desktoppc • Dec 12 '20
[ASK] How to Learn Java Spring?
I have a background in backend development using Python Django, but now I have to use Java Spring. I can do programming in Java but haven't used Spring.
But the problem is there are so many Java Spring framework like Hibernate, Security, MVC, etc. In addition, I also see that we have to define our ORM implementation like how to make the SQL query, etc. I haven't done anything like that because in Python Django the ORM implementation has been made by the framework.
I would like to learn how to make a REST API as most of the backend job required this for Junior Engineer (Fresh Graduate), how do I start? do I need to learn all java spring framework?
78
Upvotes
3
u/nekokattt Dec 12 '20 edited Dec 12 '20
More focusing on your last paragraph here, as everyone else has covered the rest nicely.
MVC is just how spring implements requests and responses for web requests that are plain old java objects (POJOs).
Think of something that is a mishmash of flask and dataclasses_json in Python.
Hibernate is a database ORM, and implements the Java Persistence API (JPA) which is most likely what you will use to convert your POJOs to SQL queries/statements and back. Think SQLAlchemy in Python.
You combine the two to be able to go from an HTTP request to a SQL query, using Java as the glue in between for making sense of how to convert things :-)
Ok, so I think there is a big misunderstanding of what Spring/Spring Boot is.
Django is a website ORM for Python that can talk with databases.
Spring is a bunch of opinionated libraries that wrap around the core spring framework, which is at its heart a fairly simple dependency iniection framework. Spring and by extension Spring Boot then wrap other libraries or make their own implementations for as many different things as possible. This currently covers web gateways, SOAP, JSON and XML libraries, validation, web sockets, HTTP via Java Servlets, HTTP via reactive programming, Kotlin/Scala/Groovy integrations, cloud provider APIs (like AWS), security for java code, security for HTTP APIs, message queues via JMS, SQL databases via JooQ or directly via JDBC, SQL via ORMs like JPA, NoSQL like MongoDB, Caches like Redis, scheduling libraries for running stuff at given times, batch processing libraries, HAL/HATEOAS integrations, templating libs like Apache Velocity/Apache Freemarker/Jinja4J, Thymeleaf; monitoring frameworks, cloud orchestration frameworks, aspect oriented development frameworks, self documenting applications that generate swagger specs, etc etc etc.
The point is, Spring does massively more than Django does because it isnt for a specific use case, it is more a specific way of programming applications as an ideaology.
You only need to learn what you are needing to use. For your example, I'd suggest a combo of:
spring-boot-starter-logging // logback logging api, like
loggingin pythonspring-boot-starter-mvc // web stuff
spring-boot-starter-validation // jsr-303 and jsr-380 validation-by-annotations
spring-boot-starter-jpa // for your SQL part of the ORM
spring-boot-starter-thymeleaf // if you need to make HTML pages
spring-boot-starter-hateoas // if you need to provide HAL responses
spring-boot-starter-security // lets you secure HTTP endpoints and other stuff
spring-boot-starter-actuator // provides a bunch of API endpoints automatically for performing health checks, etc.
lombok // optional and like marmite to some developers. Some love it, some hate it, but using it lets you massively reduce your boilerplate code with annotations that can generate the equivalent of
__init__,__str__/__repr__,__eq__,__hash__, etc from python.Once you get the gist of these, you can explore other cool things :-)
--
Hopefully that kinda explains why there are so many moving bits in spring compared to django :-)