Home » Spring » Page 3

Tag Archives: Spring

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 »

For gourmets and practioners: pick your flavour of the reactive stack with JAX-RS and Apache CXF

java-interview-questions-answers

When JAX-RS 2.1 specification was released back in 2017, one of its true novelties was the introduction of the reactive API extensions. The industry has acknowledged the importance of the modern programming paradigms and specification essentially mandated the first-class support of the asynchronous and reactive programming for the Client API. But what about the server side? It was not left ...

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 »

Quick tip: Referencing other Properties in Spring

spring-interview-questions-answers

In Spring property (or yaml) files we can reference other properties using the ${..} syntax. For example: 1 2 3 external.host=https://api.external.com external.productService=${external.host}/product-service external.orderService=${external.host}/order-service If we now access the external.productService property (e.g. by using the @Value annotation) we will get the value https://api.external.com/product-service. For example: 1 2 @Value("${external.productService}") private String productServiceUrl; // https://api.external.com/product-service This way we can avoid duplication of commonly used values in property ...

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 »

Integrating JSON Schema validation in Spring using a custom HandlerMethodArgumentResolver

In previous posts we learned about JSON Schema and how we can validate a JSON document against a JSON Schema in Java. In this post we will integrate JSON Schema validation into Spring controllers using a custom HandlerMethodArgumentResolver. We will use the same JSON document and JSON Schema as in previous posts. So, what is a HandlerMethodArgumentResolver? Handler methods in ...

Read More »

10 JdbcTemplate Examples in Spring Framework

spring-interview-questions-answers

Hello guys, If you have been using the Spring framework then you may know that the JdbcTempalte is one of the most useful classes of Spring framework. The JdbcTempalte class provides lots of convenient methods for performing database-related tasks that take the pain away from using the JDBC API. If you have worked in any real-life JAva project which connects to Database ...

Read More »

RESTful services with HATEOAS: REST APIs and Hypermedia on JVM

java-interview-questions-answers

1. Introduction So far we have spent a fair amount of time talking about the role of the hypermedia and HATEOAS in the RESTful web services and APIs, glancing over different specifications and usability aspects. It sounded like supporting hypermedia and HATEOAS is not that difficult, just pick you favorites and you are good to go! As you may guess, ...

Read More »

Using Multiple Dynamic Caches With Spring

spring-interview-questions-answers

In a third post about cache managers in spring (over a long period of time), I’d like to expand on the previous two by showing how to configure multiple cache managers that dynamically create caches. Spring has CompositeCacheManager which, in theory, should allow using more than one cache manager. It works by asking the underlying cache managers whether they have ...

Read More »