Introduction

Currently I’m working on a bigger project for a client and one of the requirements was to document the REST service, therefore we choose to use Swagger for it.

Swagger provides ways to document REST services an their API. While working with Swagger and the Swagger Maven plugin by kongchen I encountered the following error message, while trying to deploy the application:

Error message

Failed to execute goal com.github.kongchen:swagger-maven-plugin:1.1.2:generate (default) on project encrypted-project-name: null: MojoExecutionException: NullPointerException -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Unfortunately the error message wasn’t saying much and did not give any details about the error and in which line it occured.

Resource class

My resource class was looking something like this:

@Component
@Path("/report")
@Api(value = "/report", description = "Reporting")
public class ReportResource {

    private static final Logger LOG = LoggerFactory.getLogger(ReportResource.class);

    @Autowired
    ReportService reportService;

    @GET
    @Path("/report")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Report> getReport() {
        return reportService.getReport();
    }
}

Solution

The problem lies in the @Api annotation. When you don’t have any methods annotated with @ApiOperation, you will receive the above mentiond exception from Maven. So you can do two things:

Either remove the @Api from the class, what you won’t do, because you want to document the API. So the second solution is just to add the @ApiOperation annotation to the method. This will fix the issue.

So in my case the solution looked like this:

@Component
@Path("/report")
@Api(value = "/report", description = "Reporting")
public class ReportResource {

    private static final Logger LOG = LoggerFactory.getLogger(ReportResource.class);

    @Autowired
    ReportService reportService;

    @GET
    @Path("/report")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Retrieve report")
    public List<Report> getReport() {
        return reportService.getReport();
    }
}