【RESTEasy】统一错误处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35448976/article/details/82735784

为Rest服务提供统一错误处理

step1:配置统一错误类

<context-param>
 	    <param-name>resteasy.providers</param-name>
  	    <param-value>com.lfsenior.ServerErrorHandler</param-value>
</context-param>

step2:配置统一错误处理类

@Provider
public class ServerErrorHandler  implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable exception) {
        Map<String,Object> result=new HashMap<>();
        result.put("result",false);
        result.put("msg",exception.getMessage());
        return Response.ok(result, MediaType.APPLICATION_JSON).build();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35448976/article/details/82735784