Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can access to database in the resource file for API?

Maxim Olishevsky January 23, 2018

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?

1 answer

1 accepted

0 votes
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2018

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;
}

  

Maxim Olishevsky January 23, 2018

Hello,

Yes, NullPointerException in getModelById method?

I didn't add @Inject.
Thank you very much.

Suggest an answer

Log in or Sign up to answer