Home » Spring MVC

Tag Archives: Spring MVC

Improving Spring Mock-MVC tests

java-interview-questions-answers

Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I used Mock-MVC tests in various projects and in my experience they often become quite verbose. This doesn’t have to be bad. However, it often results in copy/pasting code snippets around in test classes. ...

Read More »

Reduce Repetitive Code in Spring MVC Controllers

spring-interview-questions-answers

After spending some time doing sustained engineering (a.k.a. maintaining legacy code), I ventured to reduce repetitive code in Spring MVC @Controllers. I started with an abstract base controller class. But I soon found out that it was a dead-end because @RequestMapping is not inherited from (or combined with) parent classes and/or interfaces (see Spring MVC @RequestMapping Inheritance). With some free ...

Read More »

What is Bean scope in Spring MVC framework with Example

spring-interview-questions-answers

Bean scope in Spring framework or Spring MVC is scope for a bean managed by Spring IOC container. You may know that Spring is a framework that is based on Dependency Injection and Inversion of Control and provides bean management facilities to Java application. In Spring-managed environment bean (Java Classes) are created and wired by the Spring framework. Spring allows ...

Read More »

Spring MVC – HTTP message converter

Quite often you need to provide users with the same data, but in different forms, like JSON, PDF, XLS, etc. If your application is Spring Framework based, this task can be achieved using HTTP message converters. HTTP message converters are applied when HTTP request (or its parts) needs to be converted into type required for handler method argument (see: Handler ...

Read More »

Spring MVC Binding w/o Setters

spring-interview-questions-answers

You can bind form parameters to a domain model object even if the domain model object does not have setters. Just add a @ControllerAdvice class with an @InitBinder method that configures your application to field binding via the initDirectFieldAccess() method package boottests.controllers; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.*; @ControllerAdvice class BindingControllerAdvice { @InitBinder void initBinder(WebDataBinder binder) { binder.initDirectFieldAccess(); } } Here’s how ...

Read More »

Spring with Rails’ jQuery UJS

I’ve always wanted to try to see if I could use Rails’ jQuery UJS in a Spring Boot project. The UJS in jquery-ujs stands for unobtrusive JavaScript. I really like how UJS wires event handlers to eligible DOM elements marked with HTML5 data-* attributes. I find myself wanting to see more of this approach being used in Spring Boot web ...

Read More »

Caching in Spring Boot with Spring Security

java-interview-questions-answers

In this post, I’d like to share a lesson learned by one of the teams at O&B. They were using Spring Boot with Spring Security. By default, anything that is protected by Spring Security is sent to the browser with the following HTTP header: Cache-Control: no-cache, no-store, max-age=0, must-revalidate Essentially, the response will never be cached by the browser. While ...

Read More »

Implementing White-Labelling

java-interview-questions-answers

Sometimes (very often in my experience) you need to support white-labelling of your application. You may normally run it in a SaaS fashion, but some important or high profile clients may want either a dedicated deployment, or an on-premise deployment, or simply “their corner” on your cloud deployment. White-labelling normally includes different CSS, different logos and other images, and different ...

Read More »

Dealing with Domain Objects in Spring MVC

java-interview-questions-answers

I was recently surprised by how one code base had public default constructors (i.e. zero-arguments constructors) in all their domain entities, and had getters and setters for all the fields. As I dug deeper, I found out that the reason why the domain entities are the way they are is largely because the team thinks it was required by the ...

Read More »