Java应用如何支持破折号等特殊字符?复制粘贴乱码问题求助
Alright, let's tackle this issue where your em dash (\u2014) is getting replaced by a question mark, while regular hyphens work perfectly fine. From what you described, the client-side $scope.data holds the correct value, but the server's request payload is already garbled—so the problem is happening during request transmission or initial server parsing, even though you've set UTF-8 in a few places. Here's how to fix it step by step:
Why This Happens
Em dashes are non-ASCII characters (\u2014), so if any part of the request pipeline doesn't enforce UTF-8 encoding, they'll get mangled into question marks. Your current configs (meta tag, @Consumes) are good starts, but they don't cover all critical points for XML POST requests.
Client-Side (Angular 1.5.5) Fixes
Force UTF-8 in Request Headers
Even if you assume the content type is set, Angular's$httpmight not explicitly include the charset in theContent-Typeheader. Add this to your POST call to ensure the server knows the request body uses UTF-8:$http.post('/your-api-endpoint', yourXmlData, { headers: { 'Content-Type': 'application/xml; charset=UTF-8' } });Add UTF-8 Declaration to Your XML
Make sure the XML you're sending starts with an encoding declaration. This tells the server's XML parser exactly what encoding to use:<?xml version="1.0" encoding="UTF-8"?> <!-- Your XML content here -->
Server-Side (Spring 5.2.2) Fixes
Configure XML Message Converters for UTF-8
Spring's default XML message converters might not use UTF-8 by default, even with@Consumes. Override the converters in your config class to enforce UTF-8:@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.forEach(converter -> { if (converter instanceof AbstractXmlHttpMessageConverter) { ((AbstractXmlHttpMessageConverter<?>) converter).setDefaultCharset(StandardCharsets.UTF_8); } }); } }Add a Character Encoding Filter
Install a filter to force all requests (including XML) to use UTF-8. This ensures that even if the header is missing, the server parses the body correctly:@Bean public FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilter() { CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); encodingFilter.setEncoding("UTF-8"); encodingFilter.setForceEncoding(true); // Forces encoding even if client doesn't specify FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean<>(encodingFilter); registrationBean.addUrlPatterns("/*"); // Apply to all endpoints return registrationBean; }If you're using
web.xmlinstead of Java configs, add this:<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>Check Tomcat Configuration (If Applicable)
If you're running on Tomcat, ensure it's configured to handle UTF-8. Editserver.xmland add these attributes to your Connector:<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>
How to Verify
- Use Chrome DevTools or Fiddler to inspect the request: confirm the
Content-Typeheader includescharset=UTF-8, and the request body's em dash appears correctly in the raw view. - On the server side, debug to check
request.getCharacterEncoding()returnsUTF-8instead ofnullorISO-8859-1.
These steps should ensure that the em dash travels from client to server without getting mangled into a question mark.
内容的提问来源于stack exchange,提问作者Anil Satija

