1、配置时增加
1、定义异常处理器、
@Provider
public class InvokeFaultExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable ex) {
int code;
String msg;
if (ex instanceof NotFoundException) {
//没有找到资源路径
code = 404;
msg = "404";
} else if (ex instanceof JsonParseException) {
code = 400;
msg = "json转换异常";
} else if (ex instanceof UnrecognizedPropertyException) {
code = 400;
msg = "";
} else {
code = 500;
msg = ex.getMessage();
}
ResponseData date = ResponseData.build().setStatusCode(code).setMessage(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON).entity(date).build();
}
}
2、注册到Server
@Bean
public Server jaxRsServer(ApplicationContext ctx