简体   繁体   English

@Path注释:dropwizard中的抽象资源

[英]@Path annotation: abstract resource in dropwizard

I had an idea to build abstract resource class for my application: 我有一个想法为我的应用程序构建抽象资源类:

abstract class MyAbstractResource<A> {
    MyAbstractDao dao;

    public MyAbstractResource(MyAbstractDao dao) {
        this.dao = dao;
    }

    @Path("/")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    public A create(A account) {
        return dao.create(account);
    }

    @Path("/{id}")
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    public A update(A account) {
        return dao.change(account);
    }

    @Path("/{id}")
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    public void delete(@PathParam("id") Long id) {
        dao.delete(id);
    }

    @Path("/{id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    public A get(@PathParam("id") Long id) {
        return dao.findById(id);
    }

    @Path("/")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Timed
    public List<A> getAll() {
        return dao.findAll();
    }
}

and implementations like: 和类似的实现:

@Path("/account")
class AccountResource extends MyAbstractResource<AccountDaoImpl> {
    private static final Logger LOG = LoggerFactory.getLogger(AccountResource.class)
}

, but i've faced a problem that @Path-annotation cannot be overridden or added: ,但是我遇到了无法覆盖或添加@ Path-annotation的问题:

Exception in thread "main" javax.servlet.ServletException: org.glassfish.jersey.servlet.ServletContainer-164d01ba@41f867a3==org.glassfish.jersey.servlet.ServletContainer,1,false
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:633)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:395)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:871)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:298)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:741)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:102)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.handler.RequestLogHandler.doStart(RequestLogHandler.java:140)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:232)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
at org.eclipse.jetty.server.Server.start(Server.java:387)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
at org.eclipse.jetty.server.Server.doStart(Server.java:354)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
at io.dropwizard.cli.Cli.run(Cli.java:70)
at io.dropwizard.Application.run(Application.java:73)
at io.dropwizard.Application$run.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)
at com.fappilla.web.FappillaWebService.main(MyWebService.groovy:24)
Caused by: java.lang.IllegalArgumentException: duplicate key: public java.util.List com.fappilla.web.resources.AbstractFappillaResource.getAll()
at jersey.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)
at jersey.repackaged.com.google.common.collect.RegularImmutableMap.<init>(RegularImmutableMap.java:67)
at jersey.repackaged.com.google.common.collect.ImmutableMap$Builder.fromEntryList(ImmutableMap.java:249)
at jersey.repackaged.com.google.common.collect.ImmutableMap$Builder.build(ImmutableMap.java:235)
at com.codahale.metrics.jersey2.InstrumentedResourceMethodApplicationListener.onEvent(InstrumentedResourceMethodApplicationListener.java:178)
at org.glassfish.jersey.server.internal.monitoring.CompositeApplicationEventListener.onEvent(CompositeApplicationEventListener.java:74)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:561)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:166)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:336)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:612)
... 40 more

As far as I understand, @Path-annotation is not @Inherited. 据我了解,@ Path-annotation不是@Inherited。 The question is: how can I implement methods only once for multiple DAOs and resources in such case? 问题是:在这种情况下,如何才能为多个DAO和资源仅实现一次方法? Is there any workaround? 有什么解决方法吗?

PS: I use groovy in my project, but wrote java code above, to let more people give me an advice, so both java and groovy answers are welcome. PS:我在项目中使用了groovy,但是在上面编写了Java代码,以便让更多的人给我一些建议,因此,欢迎使用java和groovy的答案。

Ok, the problem was the "@Timed"-Annotation. 好的,问题是“ @Timed”-注释。 Deleting it solved the problem. 删除它可以解决问题。

In case anyone else comes across this, another solution is to copy InstrumentedResourceMethodApplicationListener.java from https://gist.github.com/mtakaki/746dba30edfefc0ffbd5 onto your classpath - it will get picked up by the classloader over the official class from the metrics jar. 万一其他人遇到这种情况,另一个解决方案是将InstrumentedResourceMethodApplicationListener.javahttps://gist.github.com/mtakaki/746dba30edfefc0ffbd5复制到您的类路径中-它会由类加载器从度量标准jar中通过官方类进行拾取。

For more information, see: 有关更多信息,请参见:

I've verified that this fixes the problem with superclass resources which use @Timed annotations on Dropwizard 0.9.1. 我已经验证这可以解决超类资源的问题,这些超类资源在Dropwizard 0.9.1上使用@Timed批注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM