- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望我的服务客户端的使用者能够获得比标准 JAX-RS WebApplicationExceptions 更有意义的异常,这样他们就可以根据 500 异常的原因执行重试或不重试等操作。
我在服务端有一个 ExceptionMapper,它将实际的异常作为返回的异常中的响应实体,如下所示:
public Response toResponse(InternalException ex) {
return Response.status(ex.getStatus)
.entity(ex)
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}
这似乎有效。问题是,在客户端中,即使我有一个 ClientResponseFilter 从响应中提取 InternalException 并将其抛出,JAX-RS 也会将其包装在 ProcessingException 中,这违背了整个要点!
Testcase: badRequest_clientCall_modeledExceptionThrown took 0.272 sec
Caused an ERROR
Unexpected exception, expected<not.actual.package.InternalException> but was<javax.ws.rs.ProcessingException>
java.lang.Exception: Unexpected exception, expected<not.actual.package.InternalException> but was<javax.ws.rs.ProcessingException>
Caused by: javax.ws.rs.ProcessingException
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:263)
at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:709)
<Incredibly secret stuff ommitted>
Caused by: not.actual.package.InternalException
我如何真正让这个客户端抛出正确的异常,而不是将最终输出包装在 try/catch 中(我不想这样做,因为它会使重试逻辑复杂化)?
最佳答案
您可以使用spring或JavaEE的AOP( JaveEE APO example )。使用 @AroundInvoke 来调用您的服务方法。只需在您的服务方法中抛出异常即可。我建议您创建自己的异常,例如 UsernameInvalidException
、ParameterErrorException
下面是 JavaEE 的代码示例:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import java.io.IOException;
import java.lang.reflect.Field;
@YourExceptionHandler
@Interceptor
public class YourExceptionInterceptor {
@Inject
private Logger logger;
@AroundInvoke
public Object handleException(InvocationContext ctx) {
//logger.debug("hash:{}", System.identityHashCode(this));
Result returnResult = new Result();
Field resultField = null;
Object result = null;
Class<? extends Object> returnType = ctx.getMethod().getReturnType();
try
{
logger.info("method:{},return type:{}", ctx.getMethod(),
ctx.getMethod().getGenericReturnType());
returnType = ctx.getMethod().getReturnType();
result = ctx.proceed(); // this invoke your service
}
catch ( UsernameInvalidException e )
{
try
{
result = returnType.newInstance();
resultField = result.getClass().getDeclaredField("result");
if ( resultField == null )
{
return null;
}
returnResult.setResultType(ResultType.ERROR);
returnResult.setResultCode(e.getErrorCode()); // code you defined
// error text you set in UsernameInvalidException when thrown
returnResult.setText(e.getMessage());
}
catch ( Exception e1 )
{
e1.printStackTrace();
}
}
catch ( Exception e ) // catch other unexpected exceptions
{
e.printStackTrace();
}
try
{
if ( resultField != null )
{
resultField.setAccessible(true);
resultField.set(result, returnResult);
}
}
catch ( Exception e )
{
e.printStackTrace();
}
return result;
}
}
定义您的异常(exception):
public class BaseException extends RuntimeException {
protected static final String SCANPAY_EXCEPTION_CODE = "300";
protected static final String BASE_EXCEPTION_CODE = "400";
protected static final String USERNAME_INVALID_EXCEPTION_CODE = "405";
protected static final String DAO_EXCEPTION_CODE = "401";
protected static final String SERVICE_NOT_AVAILABLE_CODE = "414";
protected static final String PARAMETER_ERROR_CODE = "402";
protected String errorCode;
public BaseException() {
initErrorCode();
}
public BaseException(String message) {
super(message);
initErrorCode();
}
public BaseException(Throwable cause) {
super(cause);
initErrorCode();
}
public BaseException(String message, Throwable cause) {
super(message, cause);
initErrorCode();
}
public BaseException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
initErrorCode();
}
protected void initErrorCode() {
errorCode = BASE_EXCEPTION_CODE;
}
public String getErrorCode() {
return errorCode;
}
}
您的用户名无效异常:
@SuppressWarnings("serial")
public class UsernameInvalidExcepton extends BaseException {
@Override
@SuppressWarnings("static-access")
protected void initErrorCode() {
this.errorCode = this.USERNAME_INVALID_EXCEPTIO_CODE;
}
public UsernameInvalidException(String message) {
super(message);
}
定义自己的ExceptionHandler,它是一个注释(请参阅此处 custom annotation ):
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Inherited
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
@Documented
public @interface YourExceptionHandler {
}
定义一个异常抛出工厂(这个工厂不是必须的,它只是用来抛出新的不同的异常。当你需要抛出它时,你可以新的异常。):
public abstract class YourExceptionThrowFactory {
private YourExceptionThrowFactory() {
}
public static void throwUsernameInvalidException(String message) {
throw new UsernameInvalidException(message);
}
如何使用它?在您的服务方法中,使用注释:
@YourExceptionHandler
public UserInfo getUserInfo(String username) {
// your service code
if (username == null) {
YourExceptionThrowFactory.throwUsernameInvalidException(
"Username is not valid"); // throw your exception with your message.
}
}
然后对于客户端,它将获取您的错误代码和错误消息。
以上所有代码都是针对JavaEE的,如果你使用spring,你会找到相应的注解和技术。
关于java - JAX-RS 客户端可以抛出自定义异常类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825860/
Apache CXF 可以完成任务。但我想在没有任何 spring 依赖的情况下组合它。提前谢谢你! 最佳答案 JAX-WS 和 JAX-RS 都是 Java EE 6 的一部分.因此,您可以在不使用
Apache CXF 可以完成任务。但我想在没有任何 spring 依赖的情况下组合它。提前谢谢你! 最佳答案 JAX-WS 和 JAX-RS 都是 Java EE 6 的一部分.因此,您可以在不使用
所以我的意思是你有一个分类特征 $X$(假设你已经把它变成了整数)并说你想使用特征 $A$ 将它嵌入到某个维度中,其中 $A$ 是 arity x n_embed . 通常的做法是什么?使用 for
所以我的意思是你有一个分类特征 $X$(假设你已经把它变成了整数)并说你想使用特征 $A$ 将它嵌入到某个维度中,其中 $A$ 是 arity x n_embed . 通常的做法是什么?使用 for
我知道网上有很多文档主要描述技术差异。 但是我很想知道您更喜欢一种特定类型而不是其他类型的常见用例是什么? 这些偏好是因为集成模式/产品支持特定类型吗? 最佳答案 谢谢你的回答。然而实际用例差异以及何
我想将 CXF 与 Google Guice 集成。我已经在我的项目中使用 Guice,并且我想避免添加额外的依赖项。 CXF 是我的选择,因为要求之一是能够向服务用户提供 XML、JSON、JSON
来自 JSR-339: For simplicity, JAX-RS implementations are NOT REQUIRED to support processing groups oth
我开始使用 JAX WS 研究 Java Web 服务。我正在阅读的书的第一章展示了如何仅使用 java SE 构建和部署简单的 jax ws web 服务。特别是,Web 服务是通过 Endpoin
在 JAX 的快速入门教程中,我发现可以使用以下代码行为可微函数 fun 高效地计算 Hessian 矩阵: from jax import jacfwd, jacrev def hessian(fu
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。 public interface StreamingOutput {
有没有办法获得java.lang.reflect.Method为给定的 @Path 调用的方法(用 HttpServletRequest 注释) ? 这是我的用例:我在 Java EE 中 Filte
我接到了一家公司的任务,该任务向我发送了一台已完成所有设置的虚拟机。任务是我必须创建一个 API 来从数据库中检索人员详细信息并显示它。 问题是,当我运行应用程序时,服务器返回一个包含 hello w
我有一个 POST 方法调用,它接受很少的表单参数。我希望 JAX-RS 能够处理不存在特定表单参数的情况。 示例: @POST @Produces (MediaType.APPLICATION_JS
我有 JAX-RS 网络应用程序,我想记录从获取请求到响应的时间量。在带有 servlet 过滤器的 Spring Boot 中很容易。但是我的应用程序中的过滤器无法正常工作: @Provider p
使用以下网址。 http://doma.in/context/resource/some/.../undefined 我想获取 ../resource 之后的路径名,即 /some/.../undef
我编写了 2 个 Web 服务,一个使用 Jax-WS,一个使用 Jax-RPC。他们只是返回一个字符串。 我使用 jMeter 进行了压力测试,奇怪的是,Jax-RPC 速度更快。 我什么时候才能在
我花了几个小时在嵌入式 Jetty 9.1.0.v20131115 和 RESTEasy 3.0.5.Final 中安装自定义登录服务。我的登录服务将在数据库中查找用户并为他们分配角色。它看起来像这样
有没有办法操纵jaxws中使用的编码器。 我喜欢发送一个在网络服务请求中提交的 cdata,为此我想在这里尝试类似描述的东西:http://odedpeer.blogspot.de/2010/07/j
我想从 java 服务器 (Restful Jax-rs) 发送图像。我的客户是安卓。 @GET public Response getUserImage() { byte[] image =new
我对网络服务很陌生。我找不到 JAX-WS 和 CXF 之间的区别。 据我了解,JAX-WS是java提供的规范,CXF是实现。 如果我错了,请纠正我。 最佳答案 是的,你是对的。 JAX-WS 是基
我是一名优秀的程序员,十分优秀!