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

指定单个Controller响应字符集报错:无对应转换器问题求助

单个接口指定非UTF-8字符集报错的解决方法

问题描述

需求是为单个Controller中的某个接口指定GB2312字符集(其余接口默认使用UTF-8),通过produces="application/json;charset=GB2312"配置后,调用该接口返回错误:

"No converter for [class com.....domain.ControlResult] with preset Content-Type 'null'"

原因分析

Spring MVC默认的JSON消息转换器(如MappingJackson2HttpMessageConverter)仅默认支持UTF-8编码的application/json媒体类型。当接口指定GB2312字符集时,转换器无法匹配对应的媒体类型,导致找不到合适的转换器处理响应对象。

解决方案

方案一:扩展默认JSON消息转换器的支持类型

通过配置类扩展MappingJackson2HttpMessageConverter的支持媒体类型,添加GB2312的支持:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
        
        // 添加支持的媒体类型,包含GB2312和UTF-8
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.valueOf("application/json;charset=GB2312"));
        supportedMediaTypes.add(MediaType.valueOf("application/json;charset=UTF-8"));
        jacksonConverter.setSupportedMediaTypes(supportedMediaTypes);
        
        // 保留原有ObjectMapper配置(如有自定义配置可在此添加)
        ObjectMapper objectMapper = new ObjectMapper();
        jacksonConverter.setObjectMapper(objectMapper);
        
        converters.add(jacksonConverter);
    }
}

方案二:手动处理响应输出

绕过Spring MVC的默认消息转换机制,直接在接口中设置响应头并输出JSON字符串:

@PostMapping(value="/ConsumTransactions")
public void consumTransactions(@RequestBody SysConSum conSum,
                               @RequestHeader(name = "Device-ID") String deviceId,
                               HttpServletResponse response) throws IOException {
    ControlResult result = controlMachineService.consumTransactions(conSum, deviceId);
    
    // 设置响应的Content-Type和字符集
    response.setContentType("application/json;charset=GB2312");
    response.setCharacterEncoding("GB2312");
    
    // 将结果对象转为JSON字符串并写入响应
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonResponse = objectMapper.writeValueAsString(result);
    response.getWriter().write(jsonResponse);
}

方案三:适配FastJson消息转换器(如果项目使用FastJson)

若项目使用FastJson作为JSON处理库,需配置FastJsonHttpMessageConverter支持GB2312:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.valueOf("application/json;charset=GB2312"));
        supportedMediaTypes.add(MediaType.valueOf("application/json;charset=UTF-8"));
        fastJsonConverter.setSupportedMediaTypes(supportedMediaTypes);
        
        // 配置FastJson的字符集
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Charset.forName("GB2312"));
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        
        converters.add(fastJsonConverter);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 04:08:18