Postman请求结果乱码的相关问题_postman 乱码
一、在Postman上预览的请求结果乱码:
检查Postman的Headers配置:
如图:
其中,Content-Type根据自己的需求来修改value,如:请求结果返回的是Json格式,则将value改为 application/json;charset=UTF-8。在编写发送请求的controller时,可以设置响应头,如:
@PostMapping(\"/pu/{id}\") public void pu(@PathVariable int id, @RequestBody String message, HttpServletRequest request , HttpServletResponse response) throws UnsupportedEncodingException { request.setCharacterEncoding(\"UTF-8\");//设置编码 response.setContentType(\"application/x-www-form-urlencoded; charset=UTF-8\");//设置类型 redisTemplate.opsForValue().set(\"sse:data:\"+id, message); }
另外,Accept可以直接默认*/*,不定义发送请求和响应的格式,也可以根据自己的需求来更改,请参考:链接。发送请求时,如果body需要使用Json格式,则如下图选择:
结果:
二、在浏览器上预览请求结果乱码问题:
在浏览器上预览在Postman中的请求参数返回的请求结果乱码,如图:
只需将接受请求结果的响应头中的ContentType设置为text/html;charset=UTF-8,如下:
@Autowired private RedisTemplate redisTemplate; private static final Map map = new HashMap(); @GetMapping(\"/sub/{id}\") public SseEmitter sub(@PathVariable int id,HttpServletResponse response) { response.setContentType(\"text/html;charset=UTF-8\");//设置类型 SseEmitter emitter = new SseEmitter(); map.put(id,emitter); try { // 模拟从 Redis 中获取数据并发送事件 String eventData = (String) redisTemplate.opsForValue().get(\"sse:data:\" + id); emitter.send(SseEmitter.event().data(eventData)); emitter.complete(); } catch ( IOException e) { emitter.completeWithError(e); } finally { map.remove(id); } return emitter; }
结果:
文章到此结束!