Swagger2 设置编码格式
时间: 2023-05-16 13:06:52 浏览: 128
Swagger2 可以通过在 @RequestMapping 注解中添加 produces 和 consumes 属性来设置编码格式。其中,produces 属性用于设置响应的媒体类型,consumes 属性用于设置请求的媒体类型。例如:
@RequestMapping(value = "/example", produces = "application/json;charset=UTF-8", consumes = "application/json;charset=UTF-8")
public ResponseEntity<String> example() {
// ...
}
这样就可以设置请求和响应的编码格式为 UTF-8。
相关问题
Swagger2 配置文件中设置编码格式为 UTF-8
可以在Swagger2配置文件中通过设置produces和consumes属性来指定编码格式为UTF-8,例如:
swagger:
...
produces:
- application/json;charset=UTF-8
consumes:
- application/json;charset=UTF-8
swaggerUI乱码
SwaggerUI乱码可能是因为编码格式不正确或者字体文件缺失导致的。可以尝试在SwaggerUI的HTML文件头部添加以下meta标签:
```html
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
```
或者在CSS文件中添加以下代码:
```css
@font-face {
font-family: 'Droid Sans';
font-style: normal;
font-weight: normal;
src: local('Droid Sans'), url(https://fonts.gstatic.com/s/droidsans/v8/SlGVmQWMvZQIdix7AFQXp36IaJ7g.woff) format('woff');
}
```
其中,字体文件URL链接应根据实际情况进行修改。