gpt4 book ai didi

java - 在添加自定义注释和 MethodInterceptor 时,EnhancerByGuice 生成的类导致 Dropwizard 出错

转载 作者:行者123 更新时间:2023-12-02 04:16:00 28 4
gpt4 key购买 nike

我有一个带有以下资源文件的 dropwizard 应用程序:

@Path("/v1/")
@Api("Static Resources")
@Slf4j
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Singleton
public class StaticResourcesImpl implements StaticResources {

@Inject private SomeService someService;

@GET
@Path("/help/{id}")
@ApiOperation(value = "Give a Help page", response = Help.class)
//@Action(action = "getHelp")
public Response getHelp(@PathParam("id") String id) {
try {
return Response.ok(someService.read(id)).build();
} catch (NotFoundException e){
log.warn("Exception in reading Help for id: " + id, e );
return Response.status(HttpStatus.SC_NOT_FOUND).entity(e.getMessage()).build();
}
}

这已注册到jersey,如下所示:

environment.jersey().register(injector.getInstance(StaticResources.class));

以上作品。

但是如果我创建一个自定义注释并添加到资源(上面在 getHelp 方法之前注释):

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
/**
* This annotation is intended to be used on a method with an ActionName
*/
public @interface Action {
String action();
}

并通过bindInterceptor添加方法拦截器,如下所示:

requestStaticInjection(MyInterceptor.class);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(Action.class), new MyInterceptor());

其中 MyInterceptor 如下所示:

public class MyInterceptor implements MethodInterceptor {
@Inject
private static myService MyService;

@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Action action = methodInvocation.getMethod().getAnnotation(Action.class);
if(myService.my Condition(authRequest)) {
return methodInvocation.proceed();
}
}
return myError;
}

当我尝试运行此程序时,出现以下错误:

[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public javax.ws.rs.core.Response com.abc.demo.apis.usecase.resources.impl.StaticResourcesImpl.getHelp() and public final javax.ws.rs.core.Response com.abc.demo.apis.usecase.resources.impl.StaticResourcesImpl$$EnhancerByGuice$$69175a50.getHelp() at matching regular expression /contents/formats. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.;

详细的堆栈跟踪:

javax.servlet.ServletException: io.dropwizard.jersey.setup.JerseyServletContainer-e0f2686@72f166ff==io.dropwizard.jersey.setup.JerseyServletContainer,jsp=null,order=1,inst=false
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:691)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:427)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:760)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:374)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:848)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:287)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:101)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.handler.gzip.GzipHandler.doStart(GzipHandler.java:403)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:252)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.server.Server.start(Server.java:419)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:108)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:386)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
at io.dropwizard.cli.Cli.run(Cli.java:78)
at io.dropwizard.Application.run(Application.java:93)
at com.abc.demo.apis.Boot.main(Boot.java:111)
Caused by: org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public javax.ws.rs.core.Response com.abc.demo.apis.usecase.resources.impl.StaticResourcesImpl.getHelp() and public final javax.ws.rs.core.Response com.abc.demo.apis.usecase.resources.impl.StaticResourcesImpl$$EnhancerByGuice$$69175a50.getHelp() at matching regular expression /contents/formats. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.;
source='org.glassfish.jersey.server.model.RuntimeResource@330d557e', source='org.glassfish.jersey.server.model.RuntimeResource@439047ef']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:553)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:182)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:348)
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:345)

以下是依赖项:

    <dependency>
<groupId>com.smoketurner</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>1.3.8-1</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>1.3.8</version>
<exclusions>
<exclusion>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-migrations</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-hibernate</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>1.3.8</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hubspot.dropwizard</groupId>
<artifactId>dropwizard-guice</artifactId>
<version>0.8.4.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jackson</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jersey</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jetty</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-lifecycle</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-servlets</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.palominolabs.metrics</groupId>
<artifactId>metrics-guice</artifactId>
<version>3.1.3</version>
<exclusions>
<exclusion>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-forms</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>flipkart.feeds</groupId>
<artifactId>commons</artifactId>
<version>1.0.0</version>
<!--<scope>compile</scope>-->
<exclusions>
<exclusion>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.1.1-jre</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-annotation</artifactId>
<version>4.0.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>

无法理解为什么这会导致任何问题以及如何解决此问题,在这里和那里更改了一些内容,但没有帮助。

最佳答案

不确定这是否是完全相同的问题,但我在 Guice 和拦截器中看到了一些奇怪的东西,其中异常与实际发生的情况不匹配。在我的特定情况下,异常是不同的,但根本问题是我的拦截器的 @Inject 字段没有被注入(inject)。

所以我的拦截器抛出了 NullPointerException,但我的 Api 看到了来自其上方另一个拦截器的一些其他随机异常。我们必须解决底层(隐藏的)NPE,所以不要:

bindInterceptor(Matchers.any(), Matchers.annotatedWith(Action.class), new MyInterceptor());

我必须输入:

ServiceInfoInterceptor siInterceptor = new ServiceInfoInterceptor();
requestInjection(siInterceptor);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(ServiceInfo.class), siInterceptor);

这导致我的 ServiceInfoInterceptor 具有非 null 成员,清除 NullPointerException 并清除在其位置抛出的其他随机异常。

关于java - 在添加自定义注释和 MethodInterceptor 时,EnhancerByGuice 生成的类导致 Dropwizard 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56659943/

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