I am creating the plugin.
But I don't have access to BD from Resource files for Api.
I have table model (ModelServiceImpl.java)
@Scanned
@Named
public class ModelServiceImpl implements ModelService {
@ComponentImport
private final ActiveObjects ao;
@Inject
public ModelServiceImpl(ActiveObjects ao)
{
this.ao = checkNotNull(ao);
}
...
}
And Resource file for API(ModelResource.java):
@Path("/endpoint")
public class ModelResource {
private ModelService modelService;
public ModelResource(ModelService modelService) {
this.modelService = modelService;
}
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON})
public Response getModel(@QueryParam("id") int id) {
CacheControl cacheControl = new CacheControl();
cacheControl.setNoCache(true);
return Response.ok(getModelById(id)).cacheControl(cacheControl).build();
}
private Model getModelById(int id) {
return modelService.getById(id); //modelService is null (NullPointerException)
}
}
How can I access the database in the resource file?
Hello,
I do not quite understand what the problem is. Is it NullPointerException in getModelById method? If so then add @Inject to public ModelResource
@Inject
public ModelResource(ModelService modelService) {
this.modelService = modelService;
}
Hello,
Yes, NullPointerException in getModelById method?
I didn't add @Inject.
Thank you very much.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.