Partner – Jmix-Haulmont – NPI (cat= Spring Boot)
announcement - icon

Whether you're just starting out or have years of experience, Spring Boot is obviously a great choice for building a web application.

Jmix builds on this highly powerful and mature Boot stack, allowing devs to build and deliver full-stack web applications without having to code the frontend. Quite flexibly as well, from simple web GUI CRUD applications to complex enterprise solutions.

Concretely, The Jmix Platform includes a framework built on top of Spring Boot, JPA, and Vaadin, and comes with Jmix Studio, an IntelliJ IDEA plugin equipped with a suite of developer productivity tools.

The platform comes with interconnected out-of-the-box add-ons for report generation, BPM, maps, instant web app generation from a DB, and quite a bit more:

>> Become an efficient full-stack developer with Jmix

Course – RWSB – NPI (cat=REST/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until this Friday, after which it will permanently increase by 50$

>> GET ACCESS NOW

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we’ll learn about CharacterEncodingFilter and it’s usage in a Spring Boot application.

2. CharacterEncodingFilter

CharacterEncodingFilter is a servlet filter that helps us to specify a character encoding for requests and responses. This filter is useful when browsers do not set a character encoding or if we want a specific interpretation for requests and responses.

3. Implementation

Let’s see how we can configure this filter in a Spring Boot application.

First, let’s create a CharacterEncodingFilter:

CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);

In our example, we have set the encoding as UTF-8. But, we can set any other encoding based on the requirement.

We have also used forceEncoding attribute to enforce the encoding irrespective of its presence in request from the browser. Since this flag is set as true, the provided encoding will also be applied as response encoding.

Finally, we’ll register the filter with FilterRegistrationBean which provides configuration to register Filter instances as part of the filter chain:

FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/*");
return registrationBean;

In non-spring boot applications, we can add this filter in the web.xml file to get the same effect.

4. Conclusion

In this article, we’ve described the need for CharacterEncodingFilter and seen an example of its configuration.

As always, the complete code for this article is available over on GitHub.

Course – RWSB – NPI (cat=REST/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until this Friday, after which it will permanently increase by 50$

>> GET ACCESS NOW

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)