Wildfly 8服务器部署Maven项目丹麦字符渲染异常求助
Great question—this is a common gotcha when moving from local to dev environments, even if you didn’t explicitly tweak encoding locally. Let’s break down the likely culprits and how to fix them:
1. Wildfly Server-Level Encoding Settings
Wildfly 8 might default to a non-UTF-8 encoding on your dev server, even if your local instance picked up your OS’s UTF-8 settings automatically. Here’s what to check:
- Undertow Servlet Container Encoding: Open
standalone.xml(ordomain.xmlfor domain mode) and update the<servlet-container>section in the<undertow>subsystem:<servlet-container name="default" default-encoding="UTF-8"> <!-- existing config --> </servlet-container> - JVM System Properties: Add these to the
<system-properties>section of your server config to enforce UTF-8 at the JVM level:
Alternatively, pass them as JVM arguments when starting Wildfly:<property name="file.encoding" value="UTF-8"/> <property name="sun.jnu.encoding" value="UTF-8"/>-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8
2. Database Connection Encoding
If your app uses a database, the JDBC connection might not enforce UTF-8 on the dev server, leading to garbled characters like æ, ø, å:
- For MySQL: Update your datasource URL in
standalone.xmlto include encoding parameters:<connection-url>jdbc:mysql://your-db-host:3306/your-db?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8</connection-url> - For PostgreSQL: Ensure the database uses UTF-8 encoding, and add
?client_encoding=UTF8to the connection URL if needed. - Double-check that your database tables/columns are configured to use UTF-8 (e.g.,
utf8mb4for MySQL).
3. Maven Build Encoding
Your local machine might use UTF-8 by default, but the dev server’s OS could have a different default (like ISO-8859-1). Force consistent encoding in your pom.xml:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
This ensures all resource files, Java sources, and build outputs use UTF-8, regardless of the OS’s default.
4. Web Application Encoding Filter
Add a filter to web.xml to enforce UTF-8 for all requests and responses, overriding any server defaults:
<filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
If you’re not using Spring, use the standard servlet filter equivalent.
5. OS-Level Encoding
Check the dev server’s OS default encoding—if it’s not UTF-8, it can affect how Wildfly reads files and handles text:
- On Linux: Run these commands to verify settings:
If they don’t end withecho $LANG echo $LC_ALL.UTF-8, update them (e.g., add to/etc/profileor~/.bashrc):
Restart Wildfly after making this change.export LANG=da_DK.UTF-8 export LC_ALL=da_DK.UTF-8
Why Does This Work Locally?
Your local machine likely has UTF-8 set as the default OS encoding, and Wildfly picks that up automatically. The dev server might be running on an OS with a different default or has explicit non-UTF-8 settings in its config that you didn’t notice.
Start with checking the Wildfly server config and database connection first—those are the most common culprits. Then move to Maven and OS settings if needed.
内容的提问来源于stack exchange,提问作者Anirban

