Database Integration Testing with Testcontainers

Introduction to Testcontainers In this post we'll look at Testcontainers and how it can be used to spin up throwaway test databases for your integration tests. You'll see that Testcontainers offers an excellent alternative to in memory databases, such as H2. In memory databases have benefits in terms of speed and simplicity, but there's one fundamental drawback. Your integration tests use a database technology that is substantially different to the one your application will use in production. Testcontainers allows you to spin up the database of your choice, so that your persistence tests run against the same database technology that you'll use in production. This makes [...]

By |2021-10-27T23:29:49+01:00October 27th, 2021|Containerisation, Spring Boot, Testing|0 Comments

Spring RestTemplate Request & Response Logging

It's sometimes useful to log HTTP requests and responses when working with a Spring RestTemplate. If you need fine-grained control over exactly what's logged you can use a custom interceptor to add logging before and after the remote call. Creating an Interceptor You'll need to create a class that extends ClientHttpRequestInterceptor and implement the intercept method as shown below. @Slf4j @Component public class LoggingInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { logRequest(request, body); ClientHttpResponse response = execution.execute(request, body); logResponse(response); return response; } private void logRequest(HttpRequest request, byte[] body) throws IOException { if (log.isDebugEnabled()) { log.debug("===========================request begin================================================"); log.debug("URI [...]

By |2019-12-17T22:17:46+00:00December 17th, 2019|RestTemplate, Spring|0 Comments

Configuring a Custom ObjectMapper for Spring RestTemplate

Configuring a Custom ObjectMapper for Spring RestTemplate One of the great things about RestTemplate is its simplicity. You simply instantiate it like this... RestTemplate restTemplate = new RestTemplate(); and off you go. Under the hood Spring automatically creates and registers a number of message converters to handle various data formats for requests and responses.  A MappingJackson2HttpMessageConverter uses Jackson to map POJOs to JSON and vice versa. When the MappingJackson2HttpMessageConverter is created it's given a new instance of ObjectMapper. A default instance of ObjectMapper is fine is many case but there are times when you might want to customise the ObjectMapper used.  For example, I ran into [...]

By |2019-12-03T07:49:16+00:00December 3rd, 2019|RestTemplate|0 Comments

SOAP Web Services with Apache CXF and Spring Boot

This post is based on one I wrote a few years ago about building contract first web services with Apache CXF and Spring. The previous post didnt use Spring Boot and most of the Spring and CXF configuration was via XML. This post moves things forward a bit by using the latest version of CXF and Spring Boot. Sample App We're going to build a simple Spring Boot app that exposes SOAP web service using Apache CXF.  The service will have a single operation that takes an account number and returns bank account details. If you're impatient and want to jump ahead you can grab [...]

By |2019-10-02T08:14:28+01:00June 19th, 2019|Spring, Spring Boot, Web Services|0 Comments

Service Integration with Netflix Feign and Ribbon

The guys at Netflix have developed and open sourced (among many other things) Feign and Ribbon.  These libraries can help you as a developer, to build robust, fault tolerant service integrations.  Best of all, they've been tested in the wild by Netflix, who use both libraries extensivley in their own microservices architecture. In this post we'll look at Feign and Ribbon to see how they can be used in the context of a Spring Boot application. What is Feign? Feign is a library that helps developers create declarative HTTP clients by simply defining an interface and annotating it. At runtime, Feign creates the HTTP client [...]

By |2019-04-19T20:32:05+01:00April 16th, 2019|Micro Services, REST, Spring Boot|0 Comments

Configuring Micro Services – Spring Cloud Config Server

Configuring Micro Services – Spring Cloud Config Server Managing application configuration in a traditional monolith is pretty straight forward. Configuration is usually externalised to one or more property files that sit on the same server as the application. Externalising the configuration makes sense because it allows configuration to be updated without a rebuild and redeploy of the application. It also means that build artifacts are environment agnostic, allowing you to deploy the same artifact to dev, uat, prod etc. Configuring Micro Services The move toward micro services has created a number of new challenges when it comes to configuration. Micro services by definition are [...]

By |2019-03-19T21:28:39+00:00August 2nd, 2018|AWS, Spring Boot|0 Comments
Go to Top