我想为一个控制器的响应指定不同的字符集,所有其他控制器的响应都是utf-8

问题描述 投票:0回答:1
@RestController
@RequestMapping(value = "/xyz")
public class SysControlMachineController extends BaseController {

    @Autowired
    private ISysControlMachineService controlMachineService;

    /**
     * get server time
     */
    @PostMapping(value="/ServerTime", produces="application/json;charset=utf-8")
    public ControlResult getServerTime(@RequestBody SysServerTime serverTime,
                                       @RequestHeader("Device-ID") String deviceId) {
        return controlMachineService.getServerTime(serverTime, wtrDeviceId);
    }

    /**
     * comprehensive process
     */
    @PostMapping(value="/ConsumTransactions", produces="application/json;charset=GB2312")
    public ControlResult consumTransactions(@RequestBody SysConSum conSum,
                                        @RequestHeader(name = "Device-ID") String deviceId)  

    {
       return controlMachineService.consumTransactions(conSum, deviceId);
    }

我请求第一个getServiceTime Rest api,有成功响应;但是当我请求第二个consumTransactions api时,返回错误:

“没有预设 Content-Type 'null' 的 [class com.....domain.ControlResult] 转换器”。

java spring-boot
1个回答
0
投票

Spring Boot 默认启用“forceEncoding”。这会导致请求字符集每次在过滤器中都被覆盖并设置为 UTF-8。

所以你应该尝试设置这个

server.servlet.encoding.force = false

您也可以单独处理请求或响应

 server.servlet.encoding.request-force = false
 server.servlet.encoding.response-force = false
© www.soinside.com 2019 - 2024. All rights reserved.