You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java应用如何支持破折号等特殊字符?复制粘贴乱码问题求助

Fixing Em Dash (\u2014) Showing as Question Mark in Angular-Spring XML POST Requests

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

  1. Force UTF-8 in Request Headers
    Even if you assume the content type is set, Angular's $http might not explicitly include the charset in the Content-Type header. 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'
      }
    });
    
  2. 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

  1. 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);
                }
            });
        }
    }
    
  2. 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.xml instead 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>
    
  3. Check Tomcat Configuration (If Applicable)
    If you're running on Tomcat, ensure it's configured to handle UTF-8. Edit server.xml and 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-Type header includes charset=UTF-8, and the request body's em dash appears correctly in the raw view.
  • On the server side, debug to check request.getCharacterEncoding() returns UTF-8 instead of null or ISO-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:17:31