简体   繁体   中英

Non-English String in Spring boot

I'm trying to create a String like this in Spring Boot:

   model.setBody("Bạn đã nhận được một báo cáo mới");

but when I use

model.getBody().toString()

I received a weird String like this:

B?n ?ã nh?n ???cm?t báo cáo m?i

I tried it out on Java Application and it worked fines. I did some research on Google abou thow to set utf-8 and more but its still no help. Anyone know why it behave so weird like that? P/s: I'm using

spring_boot_version=1.5.8.RELEASE

I'm using gradle

You need to Spring's CharacterEncodingFilter in your web.xml . You need to make sure this filter is the first one in the file.

<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> 

There are multiple ways to set encoding for application.

One of the ways is set below properties in application.properties in spring-boot application.

spring.http.encoding.charset=UTF-8 # the encoding of HTTP requests/responses
spring.http.encoding.enabled=true # enable http encoding support
spring.http.encoding.force=true # force the configured encoding

For other ways see this thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM