AndServer
AndServer copied to clipboard
如何接受POST请求的GBK编码的中文
作为服务端,客户端传递过来中文字符串以GBK编码,接口默认按utf-8接收到了,我如何按GBK编码获取数据呢
å¦æä½ æ¯éè¿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) {
...
}