gpt4 book ai didi

java - 特定类的异常处理/映射

转载 作者:行者123 更新时间:2023-12-03 18:36:54 25 4
gpt4 key购买 nike

我有资源类,它本身与内部服务对话。此资源充当服务的休息 API。服务层可以抛出意外异常,因此资源应该处理那些已处理的意外异常并记录下来。我正在使用 dropwizard 框架,它又使用 Jersey 。它是这样的。

@PATH(/user)
@GET
public Response getUser(@QueryParam("id") String userId) {
assertNotNull(userId);
try {
User user = service.getUser(userId);
return Response.ok(user).build();
}
catch (MyOwnException moe) { //basically 400's
return Response.status(400).entity(moe.getMsg()).build();
}
catch (Exception e) { //unexpected exceptions

logger.debug(e.getMessage);
return Response.status(500).entity(moe.getMsg()).build();
}
}

这里的问题是我必须为每个 REST api 端点执行完全相同的异常处理。我可以为这个特定资源做某种异常映射,以便我可以将所有处理逻辑和日志记录放在那里吗?我知道我可以为 Jersey 中的特定异常构建一个映射器,但那是针对整个模块而不是单个类。

最佳答案

Afaig 你不能注册 ExceptionMapper到资源方法。我已经通过实现 DynamicFeature 来尝试过这个它正在寻找自定义注释,然后尝试注册自定义 ExceptionMapperFeatureContext .

结果令人失望:
WARNING: The given contract (interface javax.ws.rs.ext.ExceptionMapper) of class path.to.CustomExceptionMapper provider cannot be bound to a resource method.

可能不起作用:
但是...

对于资源类,这实际上很容易。只需注册您的 ExceptionMapper在您的 ResourceConfig 中为您的资源类.对我来说,它看起来像:

@ApplicationPath("/")
public class ApplicationResourceConfig extends ResourceConfig {
public ApplicationResourceConfig() {
// [...]
register(YourExceptionMapper.class, YourResource.class);
// [...]
}
}

因此,如果您可以在资源类级别上使用它,那么就这样做吧。

否则您可能需要使用方面 (但我看不出有任何理由这样做) 。示例:

看点

@Aspect
public class ResourceAspect {

Logger logger = [...]

private static final String RESOURCE = "execution(public !static javax.ws.rs.core.Response path.to.resources..*(..)) && @annotation(path.to.HandleMyOwnException)";

@Around(RESOURCE)
public Object translateRuntimeException(ProceedingJoinPoint p) throws Throwable {

try {
return p.proceed();
} catch (MyOwnException moe) {
return Response.status(400).entity(moe.getMsg()).build();
} catch (Exception e) { //unexpected exceptions
logger.debug(e.getMessage);
return Response.status(500).entity(e.getMessage()).build();
}
}

}

请注意,RESOURCE配置。在这里它适用于 none static path.to.resources 下的方法返回 Response并附有 HandleMyOwnException 注释注释。

处理MyOwnException

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HandleMyOwnException {}

资源方法

@GET
@PATH("/user")
@HandleMyOwnException
public Response getUser(@QueryParam("id") String userId) {
assertNotNull(userId);
return Response.ok(service.getUser(userId)).build();
}

pom.xml

<!-- deps -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version> <!-- or newer version -->
</dependency>

<!-- build plugins -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<versionRange>[1.7,)</versionRange>
<goals>
<goal>compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugins>
<pluginManagement>

祝你有美好的一天!

已编辑

~添加了更完整的pom.xml配置
~ 更正了 ResourceAspect 中注释丢失的路径

关于java - 特定类的异常处理/映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36370991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com