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

Http入站网关集成测试报错:UnsupportedCharsetException: cesu-8

解决Http Inbound Gateway集成测试中的cesu-8字符集不支持异常

从你的报错栈和测试代码来看,问题出在测试用的RestTemplate默认发送了cesu-8作为Accept-Charset请求头,而你的WebLogic运行环境不支持这个字符集——但生产环境的调用方并没有携带这个头,所以生产运行正常。

问题根源

Spring的HttpHeaders.getAcceptCharset()方法会尝试解析请求头中的Accept-Charset值,当遇到WebLogic JVM不支持的cesu-8字符集时,就会抛出UnsupportedCharsetException。RestTemplate默认会把JVM支持的所有字符集都加入Accept-Charset头,你的测试环境JVM可能包含cesu-8,但WebLogic端不支持该字符集,因此触发了异常。

解决方案

在测试代码中显式设置Accept-Charset头为UTF-8,覆盖RestTemplate的默认行为即可解决问题。修改后的测试代码如下:

@Test 
public void receiveMessage() throws IOException { 
    final String xml = testUtil.getTestDocumentAsString("/serviceTestData/document.xml"); 
    final RestTemplate template = new RestTemplate(); 
    template.getMessageConverters() 
        .add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); 
    
    final HttpHeaders headers = new HttpHeaders(); 
    MediaType mediaType = new MediaType("text", "xml", StandardCharsets.UTF_8); 
    headers.setContentType(mediaType); 
    // 显式指定Accept-Charset为UTF-8,避免默认包含不支持的字符集
    headers.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
    
    final String credentials = this.user + ":" + this.pass; 
    final byte[] message = credentials.getBytes("UTF-8"); 
    headers.add("Authorization", "Basic " + DatatypeConverter.printBase64Binary(message)); 
    
    final HttpEntity request = new HttpEntity(xml, headers); 
    final ResponseEntity<Ack> result = template.postForEntity(url, request, Ack.class); 
    System.out.println(result.getBody().getStatus()); 
}

额外说明

如果你不想每次都手动设置请求头,也可以通过配置RestTemplate的默认消息转换器或使用RestTemplateBuilder来全局指定字符集,但直接设置Accept-Charset是解决当前问题最直接的方式。

内容的提问来源于stack exchange,提问作者Dan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:01:39