吃一筐萝卜 2023-04-11 11:44 采纳率: 42.9%
浏览 18

swagger和自定义异常都中文乱码

springboot使用swagger2中文乱码,自定义异常的中文也乱码。
部分代码:

@Configuration
@EnableSwagger2
//@Component
public class SwaggerConfig implements WebMvcConfigurer
{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = converter.getObjectMapper();
        // 生成JSON时,将所有Long转换成String
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        // 时间格式化
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        // 设置格式化内容
        converter.setObjectMapper(objectMapper);
        converters.add(0, converter);
    }
    @Bean
 public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
         .groupName("webApi")
         .apiInfo(webApiInfo())
         .select()
         .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
         .paths(Predicates.not(PathSelectors.regex("/error.*")))
         .build();
       }
    private ApiInfo webApiInfo(){

         return new ApiInfoBuilder()
         .title("仓库管理系统")
         .description("本文档描述了仓库管理系统接口定义")
         .version("1.0")
        .contact(new Contact("Helen", "http://atguigu.com",
                "55317332@qq.com"))
         .build();
         }
 }

自定义异常

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WNNException extends RuntimeException{
    private Integer code;//异常状态码
    private String msg;//异常信息
    private boolean status;
}

配置类中配置异常类

//配置
@ExceptionHandler(WNNException.class)
    public R error(WNNException e) {
        e.printStackTrace();
    return R.ok().message(new String(e.getMsg())).code(e.getCode()).success(e.isStatus());
    }

img

img

  • 写回答

2条回答 默认 最新

  • 皮蛋不吃粥 2023-04-11 13:09
    关注

    设置一下idea的编码格式 设置成UTF-8

    评论

报告相同问题?

问题事件

  • 修改了问题 4月11日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop
  • ¥15 共享文件夹会话中为什么会有WORKGROUP
  • ¥15 关于#python#的问题:使用ATL02数据解算光子脚点的坐标(操作系统-windows)
  • ¥115 关于#python#的问题:未加密前两个软件都可以打开,加密后只有A软件可打开,B软件可以打开但读取不了数据
  • ¥15 在matlab中Application Compiler后的软件无法打开
  • ¥15 想问一下STM32创建工程模板时遇到得问题
  • ¥15 Fiddler抓包443