网站维护更新,购物车按钮颜色wordpress,湖南省建设干部学校 网站,seo证书考试网站当您使用JAX-RS创建REST服务时#xff0c;通常要么不返回任何内容#xff08;例如HTTP 201/2/4等#xff09;#xff0c;要么返回某些数据#xff08;可能采用JSON格式#xff08;因此HTTP 200#xff09;#xff0c;或者返回某些异常/错误#xff08;例如HTTP 4xx或5… 当您使用JAX-RS创建REST服务时通常要么不返回任何内容例如HTTP 201/2/4等要么返回某些数据可能采用JSON格式因此HTTP 200或者返回某些异常/错误例如HTTP 4xx或5xx 。 我们通常将运行时异常转换为某些HTTP 5xx将已检查异常转换为某些4xx。 因为我们要保持边界整洁所以当我们将Exception转换为HTTP响应时我们不会在响应的主体中包含完整的Java stacktrace。 我们通常只添加带有HTTP 5xx有时是4xx响应的“ REASON”标头。 但是这意味着我们的大多数ExceptionMappers看起来都差不多类似这样 Providerpublic class SomeExceptionMapper implements ExceptionMapperSomeException {Overridepublic Response toResponse(SomeException exception) {return Response.status(500).header(reason, exception.getMessage()).build();}}使用MicroProfile Config API 我们可以使用MicroProfile Config API创建一个可配置的Exception Mapper它允许使用者将Exception配置为HTTP响应代码映射。 我们的Provider将处理所有运行时异常 Providerpublic class RuntimeExceptionMapper implements ExceptionMapperRuntimeException {// ...} 我们 Inject配置和提供程序 Injectprivate Config config;Context private Providers providers; 当我们实现toResponse方法时我们会在配置中查看此Exception类的映射 OverrideProduces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})public Response toResponse(RuntimeException exception) {return handleThrowable(exception);}private Response handleThrowable(Throwable exception) {if(exception instanceof WebApplicationException) {return ((WebApplicationException) exception).getResponse();}if(exception!null){String configkey exception.getClass().getName() STATUS_CODE_KEY;OptionalInteger possibleDynamicMapperValue config.getOptionalValue(configkey,Integer.class);if(possibleDynamicMapperValue.isPresent()){int status possibleDynamicMapperValue.get();// You switched it offif(status0)return handleNotMapped(exception);String reason getReason(exception);log.log(Level.FINEST, reason, exception);return Response.status(status).header(REASON, reason).build();} else if(exception.getCause()!null exception.getCause()!null providers!null){final Throwable cause exception.getCause();return handleThrowable(cause);} else {return handleNotMapped(exception);}}return handleNullException();} 这里有完整的示例 我们还将向上处理异常链直到获得映射或者默认为正常的500错误。 因此我们可以为映射添加配置如下所示 ## 503 Service Unavailable: The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException/mp-jaxrs-ext/statuscode503## 401 Unauthorized (RFC 7235): Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.javax.ws.rs.NotAuthorizedException/mp-jaxrs-ext/statuscode401 在上面的示例中我们将CircuitBreakerOpenException来自MicroProfile容错API映射到503将NotAuthorizedException映射到401。 屏幕截图示例 用作库。 您还可以将所有这些捆绑在一个jar文件中以供您的任何项目使用。 我在maven Central和github中提供了上述内容因此您也可以直接使用它。 只需将其添加到您的pom.xml dependencygroupIdcom.github.phillip-kruger.microprofile-extensions/groupIdartifactIdjaxrs-ext/artifactIdversion1.0.9/version/dependency 它带有一些预定义的映射但是您可以在配置中覆盖它。 翻译自: https://www.javacodegeeks.com/2018/08/jax-rs-exceptionmapper-config.html