Jax-rs,JavaEE7,Java8:SystemInjecteeImpl中没有可用于注入的对象

时间:2017-02-08 18:48:27

标签: rest java-8 jax-rs java-ee-7 payara

我是一家中型软件公司的软件工程师,他正在安静地休息一段时间。关于Java8接口中的默认方法,它们很酷。所以我决定使用这个默认行为来实现CRUD功能,每个功能都在它自己的界面( ICreateResource )中分开。在CRUD方法中执行的逻辑由一个额外的类提供,该类也被声明为接口 IResourceStateControl

所以这就是问题所在。将实现 IResourceStateControl RatingResourceStateControl 注入 RatingResourceStateBean ,实现 ICreateResource 异常"没有对象可用注入SystemInjecteeImpl"在提出请求时被提出。

public interface ICreateResource
{
    IResourceStateControl getResourceStateControl();

    @POST
    @Consumes(APPLICATION_JSON)
    @Produces(COLLECTION_JSON)
    default Response post(@Context UriInfo uriInfo, ApplicationState representation)
    {
        try
        {
            Collection collection = getResourceStateControl().post(uriInfo, representation);
            return Response.created(collection.getHref().get()).entity(collection).build();
        }
        catch (Exception exception)
        {
            throw new WebApplicationException(exception.getMessage(), exception, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
}

@Dependent
public class RatingResourceStateControl implements IResourceStateControl
{

    @Override
    public Collection get(UriInfo uriInfo, int start, int size, long parentResourceId)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection get(UriInfo uriInfo, long id)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection post(UriInfo uriInfo, ApplicationState representation)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Boolean delete(long id)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection put(UriInfo uriInfo, long id, ApplicationState representation)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection patch(UriInfo uriInfo, long id, ApplicationState representation)
    {
        // TODO Auto-generated method stub
        return null;
    }
}
@Stateless
@Path("/" + RATINGS_PATH)
public class RatingResourceStateBean implements ICreateResource
{
    @Inject
    private RatingResourceStateControl ratingResourceControl;

    @Override
    public IResourceStateControl getResourceStateControl()
    {
        return ratingResourceControl;
    }
}

但是当使用抽象类来提供功能时,一切都有效。

public abstract class AbstractResourceState
{
    protected abstract IResourceStateControl getResourceStateControl();

    @Context
    private UriInfo uriInfo;
    @Context
    private HttpServletRequest httpServletRequest;

    @POST
    @Consumes(APPLICATION_JSON)
    @Produces(COLLECTION_JSON)
    public Response post(ApplicationState representation)
    {
        try
        {
            Collection collection = getResourceStateControl().post(uriInfo, representation);
            return Response.created(collection.getHref().get()).entity(collection).build();
        }
        catch (Exception exception)
        {
            throw new WebApplicationException(exception.getMessage(), exception, Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
}

@Stateless
@Path("/" + RATINGS_PATH)
public class RatingResourceStateBean extends AbstractResourceState
{
    @Inject
    private RatingResourceStateControl ratingResourceControl;

    @Override
    protected IResourceStateControl getResourceStateControl()
    {
        return ratingResourceControl;
    }
}

api正在使用抽象类方法,但通过简单地实现适当的接口来控制CRUD方法可用是非常好的。一切都部署在payara 4.1.1应用服务器上。

最诚挚的问候 鲁迪

1 个答案:

答案 0 :(得分:0)

您正在尝试将JAX-RS资源@Context UriInfo uriInfo注入ICreateResource.post,这是一种默认方法。由于JAX-RS API基于Java 7,因此您需要小心使用默认方法,因为规范没有说明它们。这完全取决于反射API如何公开有关默认方法的信息。

问题还可能是您覆盖默认方法,但您没有复制@Context注释。当服务器扫描类时,它不会在方法签名中看到任何@Context注释(就好像默认方法不存在一样)。