On my current project we use Java 8, Spring 4.3 and Tomcat 7.0 as application server.
After the scheduling functionality was added to the project the application server did not shut down any more, it hung till the end of time.
I like to use the default Java implementations when possible so the configured scheduler was the java.util.concurrent.ScheduledThreadPoolExecutor
.
Continue reading →
In general a REST service serves JSON document formats.
In Spring Boot it is even the standard without having to configure anything.
Over HTTP we have the ability to send a Content-Type as header to instruct the server to return a certain document format (Mime type).
Besides handling JSON, XML is another common document format.
But what if we want to serve or read a YAML document format? This tutorial provides the required steps to be able to handle objects in YAML format.
To be able to send a YAML Content-Type with the header, and to be able to serialize and deserialize with the existing ObjectMappers of Jackson.
This tutorial will use Spring Boot, Jackson and a YAML dataformat extension library for Jackson.
Continue reading →
The default JSON error body of Spring Boot can be customized or extended by defining our own ErrorAttributes
implementation.
In Spring Boot we get an error response JSON format for free.
Any thrown Exception is automatically translated to this JSON format and returned with a corresponding HTTP status.
Continue reading →
In Spring we use the @EnableAutoConfiguration
each time when we use the @SpringBootApplication
annotation.
If we look at the @SpringBootApplication
we can see that this automatically enables the @EnableAutoConfiguration
.
This last mentioned annotation triggers all the auto-configuration enabled configurations on the classpath.
We can write an auto-configuration enabled @Configuration
ourself in only two steps.
Continue reading →
In Spring MVC we get some method argument types resolved by default and injected in Spring MVC controller methods.
Some examples are Model
, Locale
and OutputStream
.
What if we want to inject a custom argument in Spring MVC controller methods?
In this example we extract the X-Application-Version
HTTP header from the request and inject that as a method argument called version
.
Our controller class will look like the following:
Continue reading →
Recently I wanted to use the Tuckey UrlRewriteFilter. It is described as: A Java Web Filter for any compliant web application server, which allows you to rewrite URLs before they get to your code.
Continue reading →
I would like to show different ways of using Spring's @Autowired
annotation: Constructor, Method and Field autowiring.
The examples I show are all a form of byType
autowiring mode (constructor
autowiring mode is Analogous to byType
). Take a look at the Spring Reference guide for more information on the Autowiring modes.
Continue reading →
We have to deal with legacy code, even when we would like to use the best and newest technologies available. Imagine the new code is written with the newest technologies of the Spring Framework and the legacy code is not written in Spring at all. Then using Spring managed Beans in non-managed Spring objects is one of the patterns we have to deal with. The legacy code has non-managed Spring objects, while the code we want to reference to is a Spring managed Bean. How do we solve this problem?
Continue reading →
So many men, so many minds. When we are implementing software for different customers we sometimes need to handle various requirements for the same project. For example Customer A needs SAML authentication and customer B needs LDAP authentication. With Spring Profiles (available from Spring 3.1) we are able to provide a way to segregate parts of our implemented application configuration. This blog will help us to make certain code or rather certain Spring beans only available for specific requirements. For example the example used in this blog can be used to activate the required authentication provider for the provider manager when using Spring Security. Profiles can be configured by annotations and/or by xml. Annotations @Component or @Configuration annotated beans can contain the annotation @Profile to only load them in a certain environment.
Continue reading →
While writing unit tests, you often have to mock dependencies like services or controllers. Often a constructor is used to autowire the dependencies as shown in the example below. In the Test class I instantiated the ContactService using a contactRepository Mock object
Continue reading →