Postingan

Menampilkan postingan dengan label spring mvc

Difference between @Component, @Service, @Controller, and @Repository in Spring

Gambar
Before you learn the difference between @Component, @Service, @Controller, and @Repository annotations in Spring framework, it's important to understand the role of @Component annotation in Spring. During initial release of Spring, all beans are used to be declared in an XML file. For a large project, this quickly becomes a massive task and Spring guys recognize the problem rather quickly. In later versions, they provide annotation-based dependency injection and Java-based configuration. From Spring 2.5 annotation-based dependency injection was introduced, which automatically scans and register classes as Spring bean which is annotated using @Component annotation. This means you don't declare that bean using the <bean> tag and inject the dependency, it will be done automatically by Spring. This functionality was enabled and disabled using <context:component-scan> tag. Now that you know what does @Component annotation does let's see what does @Service, @Controlle...

3 Ways to Learn Spring Core, Spring MVC, Spring Security, and Spring Boot Framework

Gambar
If you are a Java developer and want to learn Spring framework then you have come to the right place. In this article, I will share three ways to learn the Spring framework, but before that let's understand what is a spring framework and why Java developers should learn Spring framework? Well, Spring is a framework that assists you to develop Java application by following some best practices, particularly the principle of dependency injection and inversion of control. As per this principle, instead of class asking for its dependency, the framework provides them at runtime.  Following these principles improves your code quality because it reduces coupling between different parts of your applications like between modules and classes, which makes it easier to test and develop. Because of that goodness and several other helpful features, the Spring framework has become a standard way to develop Java application, both core Java and Java Web application, particularly using Spring MVC, wh...

What is the Role of InternalResourceViewResolver in Spring MVC? Interview Question

Gambar
Earlier, I have explained to you about how Spring MVC works internally and how it process HTTP request coming to your web application. One of the important parts of that processing was view resolution, which is handled by the ViewResolver interface. In this article, you'll learn more about it by explaining the InternalResourceViewResolver class. The InternalResourceViewResolver is an implementation of ViewResolver in Spring MVC framework which resolves logical view name e.g. "hello" to internal physical resources e.g. Servlet and JSP files e.g. jsp files placed under WEB-INF folder. It is a subclass of UrlBasedViewResolver, which uses "prefix" and "suffix" to convert a logical view name returned from Spring controller to map to actual, physical views. For example, if a user tries to access /home URL and HomeController returns "home" then DispatcherServlet will consult InternalResourceViewResolver and it will use prefix and suffix to find the ...

What is the Use of DispatcherServlet in Spring MVC Framework?

Gambar
If you have worked with Spring MVC then you should know what is a DispatcherServlet? It's actually the heart of Spring MVC, precisely the C of MVC design pattern or Controller. Every single web request which is supposed to be processed by Spring MVC goes through DispatcherServlet. In general, its an implementation of Front Controller Pattern which provides a single point of entry in your application. It handles all incoming requests. It is also the bridge between Java and Spring. Btw, the DispatcherServlet is like any other Servlet is declared in the web.xml with a URL pattern but the only special thing is that the URL pattern for dispatcher servlet is enough to map every single web request to DispathcherServlert. It is responsible for request handling by delegating requests to additional components of Spring MVC e.g. actual controller classes i.e. those which are annotated using @Controller or @RestController (in case of RESTful Web Services), Views, View Resolvers, handler mapper...

Differences between @RequestParam and @PathVariable annotations in Spring MVC?

Gambar
The Spring MVC framework, one of the most popular frameworks for developing a web application in Java world also provides several useful annotations to extract data from the incoming request and mapping the request to the controller, e.g., @RequestMapping, @RequestParam, and @PathVariable. Even though both @RequestParam and @PathVariable is used to extract values from the HTTP request, there is a subtle difference between them, which makes them a useful question from an interview and spring certification point of view. We'll examine the subtle difference between @RequestParam and @PathVaraible in this article. As the name suggests, @RequestParam is used to get the request parameters from URL, also known as query parameters, while @PathVariable extracts values from URI. For example, if the incoming HTTP request to retrieve a book on topic "Java" is http://localhost:8080/shop/order/1001/receipts?date=12-05-2017, then you can use the @RequestParam annotation to retrieve the ...

How Spring MVC Makes Easy to Create RESTful Web Services in Java

Gambar
REST has now become a standard way to develop web services and when it comes to Java, there are many frameworks and library available e.g. JAX-RS, Restlet, Jersey, RESTEasy, Apache CFX etc, but I encourage Java developers to use Spring framework to develop RESTful web services. But, some of you might ask, why use Spring Framework to develop RESTful web services in Java? What is the advantage and why it's better than other frameworks and libraries available out there? Well, the most important reason I think to use Spring for developing RESTful web service is that you can use your Spring MVC experience to develop RESTful web services. This is one of the biggest advantage i.e. leveraging your years of experience on Spring MVC to expose your application as REST APIs. Another reason is that Spring has excellent support for developing RESTful web services . In last a couple of versions, starting from Spring version 3.0, it has provided a lot of enhancements to Spring MVC to provide firs...

How Spring MVC Framework works? How HTTP Request is processed?

Gambar
One of the frequently asked Spring MVC Interview questions is about explaining the flow of web request i.e. how an HTTP request is processed from start to end. In other words, explaining the flow of request in Spring MVC . Since many of my readers ask this question time and again, I thought to summarize the flow of request processing in a short article. It all starts with the client, which sends a request to a specific URL. When that request hits the web container e.g. Tomcat it look into web.xml and find the Servlet or Filter which is mapped to that particular URL. It the delegate that Servlet or Filter to process the request. Since Spring MVC is built on top of Servlet, this is also the initial flow of request in any Spring MVC based Java web application. Remember, Web container e.g. Tomcat is responsible for creating Servlet and Filter instances and invoking their various life-cycle methods e.g. init(), service(), destroy(). In the case of an HTTP request, HttpServlet handles that a...

Top 5 Books to Learn Spring framework and Spring MVC for Java Programmers

Gambar
Spring and Spring MVC is one of the most popular Java frameworks, and most of the new Java projects use Spring these days. Java programmer often asks questions like which books are good to learn Spring MVC or What is the best book to learn Spring framework etc. Actually, there are many books to learn Spring and Spring MVC, but only certain books can be considered good because of their content, examples, or the way they explained the concept involved in the Spring framework. Similar toTop 5 books on Java programming, we will some good books on Spring in this article, which not only help beginners to start with Spring but also teaches some best practices. To learn a new technology or a new framework, probably the best way is to start looking for documentation provided, and Spring Framework is no short on this. Spring offers excellent, detailed documentation to use various features of the Spring framework, but despite that, nothing can replace a good book. Luckily both Spring and Spring...

How to Consume JSON from RESTful Web Service and Convert to Java Object - Spring RestTemplate Example

Gambar
So far, I have not written much about REST and RESTful web service, barring some interview questions like REST vs. SOAP, which is thankfully very much appreciated by my readers and some general suggestions about the best books to learn REST in the past. Today I am going to write something about how to consume JSON from a RESTful Web Service in Java using Spring Framework. I will also talk about how to convert the JSON to Java objects using Jackson. You will also learn about the RESTTemplate class from the Spring MVC framework, and how you can use it to create a REST client in Java in just a few lines of code. Similar to its predecessors JdbcTemplate and JmsTemplate, the RestTemplate is another useful utility class that allows you to interact with RESTful web services from a Java application built using the Spring framework. It's a feature-rich and supports almost all REST methods like the GET, POST, HEAD, PUT or DELETE, though we'll only use the GET method in this article to co...

Top 20 Spring MVC Interview Questions for Java Developers - Answered

Gambar
The Spring MVC framework is one of the most popular Java frameworks for developing web applications. If you have been working in Java and the developing web-based application then there is a good chance that you have already used Spring MVC in your project. In the last decade, it has become the de facto framework for developing Java web applications. Spring MVC is based on classic MVC (Model-View-Controller) design pattern but it is much more than that. It leverages, Spring framework's strength in terms of dependency injection and Inversion of control and promotes loosely coupled architecture, similar to the Spring framework itself. Because of its immense popularity and usefulness, most of the Java development job requires a good knowledge of Spring and Spring MVC. There is a lot of demand for a good Java developer with good knowledge and experience in Spring and Spring MVC. One way to prepare yourself for s...