Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何接受POST请求的GBK编码的中文 #528

Open
HelloMaSH opened this issue Jan 4, 2024 · 1 comment
Open

如何接受POST请求的GBK编码的中文 #528

HelloMaSH opened this issue Jan 4, 2024 · 1 comment
Labels

Comments

@HelloMaSH
Copy link

作为服务端,客户端传递过来中文字符串以GBK编码,接口默认按utf-8接收到了,我如何按GBK编码获取数据呢

@yanzhenjie
Copy link
Owner

如果你是通过MessageConverter解析的数据,在convert方法识别客户端数据编码:

@Converter
public class AppMessageConverter implements MessageConverter {

    ...

    @Nullable
    @Override
    public <T> T convert(@NonNull InputStream stream, @Nullable MediaType mediaType, Type type) throws IOException {
        Charset charset = mediaType == null ? null : mediaType.getCharset();
        if (charset == null) {
            return JsonUtils.parseJson(IOUtils.toString(stream), type);
        }
        return JsonUtils.parseJson(IOUtils.toString(stream, charset), type);
    }
}

如果没有使用使用MessageConverter,在你api对应的方法中拿到HttpRequest后自行转换:

@RestController
class TestController {

    @RequestMapping(
        path = "/testCharset",
        method = {RequestMethod.POST},
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Object testCharset(HttpRequest request) {
        ...
    }

@yanzhenjie yanzhenjie added the bug label Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants