Custom REST API problem with @Inject @Named, not showing my apis at all

I am developing rest plugin for jira atlassian and I have this kind of problem. every time I want that my apis are showing up in REST API Browser I need to add empty constructor to my new api in this format

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/import-issues")
public class ImportIssuesRestResource {
    private ImportIssuesAction importIssuesAction;

    public ImportIssuesRestResource() {
    }

    public ImportIssuesRestResource(final ImportIssuesAction importIssuesAction) {
        this.importIssuesAction = importIssuesAction;
    }

    @GET
    @Path("issueNumber")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getFields() {
        return Response.ok().entity(importIssuesAction.getIssueNumber()).build();
    }
}

However what I want is something like this

@Scanned
@Path("/import-issues")
public class ImportIssuesRestResource{
    private ImportIssuesAction importIssuesAction;

    @Inject
    public ImportIssuesRestResource(final ImportIssuesAction importIssuesAction) {
        this.importIssuesAction = importIssuesAction;
    }

    @GET
    @Path("issueNumber")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getFields() {
        return Response.ok().entity(importIssuesAction.getIssueNumber()).build();
    }
}

with injected constructor and @Scanned. I get this error when I am using the second version of code with @Named in ImportIssuesAction. I have tried also @Scanned.

Error creating bean with name 'rest.ImportIssuesRestResource': Unsatisfied dependency expressed through constructor argument with index 0 of type [action.ImportIssuesAction] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

The others custom implemented working api have similar injected constructor and using annotations @Scanned or @Named. I need my api real-time getting information from class. However currently I am getting NullPointerException from empty constructor in rest of my public getter getIssueNumber. When I am using @Named @Inject combination the all of my custom apis are hidden. I have tried also

in atlassian-plugin.xml. In atlassian-plugin I have also CSMT Rest Resources Can somebody help with implementing custom REST API?

Hi, @jirajira

try this:

@Named
@Provider
@Path("/import-issues")
public class ImportIssuesRestResource

Best regards

Hi,
Where is the ImportIssuesAction class come from?
In order to inject ImportIssuesAction into your class, it has to be a spring component first.

I suggest you use SpringScanner version 2, so the overview should look like this:

  1. The ImportIssuesAction must be a Spring Component (annotated with @Component or @Service)
  2. The ImportIssuesRestResource must be a Spring Component (annotated with @Component)
  3. The constructor of ImportIssuesRestResource must be annotated with @Autowired (of course you can use @Inject but I’m not recommend

Note: With SpringScanner, you can use @Scanned instead of @Component, and @Inject instead of @Autowired. But with my experience, I always use the combination of @Component (or @Service) along with @Autowired to keep the consistency in the code (the code convention for my team).

Note 2: The annotations should come from these package:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

I have tried both solutions, but
it won’t show in REST API browser and also all
of my other rest points disappeared. The problem
must be in @Inject / @Autowire of constructor
of ImportIssueAction which is my custom class
annotated with @Scanned.

Try change @Path(“issueNumber”) to @Path("/issueNumber")