Spring MVC UTF-8 Encoding
Ok guys I found the reason for my encoding issue.
The fault was in my build process. I didn't tell Maven in my pom.xml
file to build the project with the UTF-8 encoding. Therefor Maven just took the default encoding from my system which is MacRoman and build it with the MacRoman encoding.
Luckily Maven is warning you about this when building your project (BUT there is a good chance that the warning disappears to fast from your screen because of all the other messages).
Here is the property you need to set in the pom.xml
file:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
Thank you guys for all your help. Without you guys I wouldn't be able to figure this out!
Spring mvc encoding and ??? symbols instead of utf-8 symbols in html
By seeing your code I can say that you are using thymeleaf as view technology.
Refer this thread.
Property characterEncoding
should be explicitly set for templateResolver
and ThymeleafViewResolver
:
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
...
<property name="characterEncoding" value="UTF-8"/>
...
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
...
<property name="characterEncoding" value="UTF-8"/>
...
</bean>
Or using annotation.
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
Spring MVC UTF-8 Character Encoding
To set the encoding of all JSPs to UTF-8 add this snippet to web.xml:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
see here https://sorenpoulsen.com/utf-8-encoding-a-jsp-with-spring-mvc
UTF-8 encoding in Spring MVC
Special characters issue, i replaced :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
by
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
and i was able to persist special characters as i want..
Force UTF8 encoding in html form with Spring MVC Web Application
I found the solution myself. The problem lies in the initialiation of the characterEncodingFilter.
In a web.xml you do the following:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Since with Spring 5 Java-based Annotation Configuration is favored, according to the documentation you can do the following in your WebApplicationInitializer which is much simpler than the code above:
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new HiddenHttpMethodFilter(),
new CharacterEncodingFilter("UTF-8", true, true) };
}
BUT this is NOT working properly with every HttpRequest coming in!!!
The solution is to not use the provided convenient way to automatically map the filter to the servlet.
Instead you must:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter("UTF-8", true, true));
filterRegistration.addMappingForUrlPatterns(null, false, "/*");
filterRegistration = servletContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter() );
filterRegistration.addMappingForUrlPatterns(null, false, "/*");
super.onStartup(servletContext);
}
And here you have your filter mapping back in sight and this works perfect!
UTF-8 encoding issue with Thymeleaf Spring MVC
Unfortunatelly I have to answer my own question.
To read UTF-8 data from properties file, just use encoding property
@Component
@PropertySource(value = "classpath:config/data.properties", encoding = "UTF-8")
basically what helped was adding some additional method calls to the configureViewResolvers
method:
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8"); // <- this was added
resolver.setForceContentType(true); // <- this was added
resolver.setContentType("text/html; charset=UTF-8"); // <- this was added
registry.viewResolver(resolver);
}
and additionaly in configure(HttpSecurity http)
method I've changed the way how filter is added to this:
@Override
protected void configure(HttpSecurity http) throws Exception {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
http.authorizeRequests().anyRequest().anonymous()
.antMatchers("/login**", "/*.js", "/*.css", "/*.svg" ).permitAll()
// ... some other config
.invalidateHttpSession(true)
.permitAll()
.and()
.addFilterBefore(filter, CsrfFilter.class); // <- this was added
}
How to configure UTF-8 in Spring Boot 2 with Spring MVC & Security, Jasig CAS and JSP views?
After unsuccessfully attempting to fix this using multiple additional approaches, my colleague suggested to have a look if there is anything from CAS where character encoding can be set.
Turns out a Cas30ServiceTicketValidator
bean was the offender as it somehow managed to override settings from all the approaches listed in the question (and several more). This is the version that ended up working:
@Bean
public TicketValidator ticketValidator() {
Cas30ServiceTicketValidator validator = new Cas30ServiceTicketValidator("https://example.com/cas");
validator.setEncoding("UTF-8"); //This had to be added
return validator;
}
No additional changes were required after this, the rest was configured more or less according to Baeldung's Spring Security CAS SSO tutorial.
Related Topics
How to Handle Calendar Timezones Using Java
How to Specify the Schema When Connecting to Postgres with Jdbc
Differencebetween "Class.Forname()" and "Class.Forname().Newinstance()"
Java Filewriter with Append Mode
Determining If an Object Is of Primitive Type
How to Detect a Loop in a Linked List
Convert an Array of Primitive Longs into a List of Longs
Resume Http File Download in Java
How to Run .Jar File by Double Click on Windows 7 64-Bit
Java8 Lambdas VS Anonymous Classes
How to Preserve Insertion Order in Hashmap
How to Achieve Method Chaining in Java
Why to Use Interfaces, Multiple Inheritance VS Interfaces, Benefits of Interfaces
Parsing Iso-8601 Datetime with Offset with Colon in Java
Java.Util.Date Format Conversion Yyyy-Mm-Dd to Mm-Dd-Yyyy
What Should I Set Java_Home Environment Variable on MACos X 10.6