Bruno Camargo Bruno Camargo - 3 months ago 27
reST (reStructuredText) Question

Implement a interface of a webservice

I have a maven project that creates a CRUD webservice using REST.
If I use this:

@GET
@Path("/getallfornecedores")
@Produces("application/json;")
public Fornecedor getAllFornecedores(){
Fornecedor f = new Fornecedor();
f.setName("Bruno");
return f;
}


My code works fine. But I want to use a interface implementation, so I did this:

private ICrud crud;

@GET
@Path("/getallfornecedores")
@Produces("application/json;")
public Fornecedor getAllFornecedores(){
return crud.getAllFornecedores();
}


The interface:

public interface ICrud {
public Fornecedor getAllFornecedores();
}


And the implementation:

public class Crud implements ICrud{
public Fornecedor getAllFornecedores(){
Fornecedor fornecedor = new Fornecedor();
fornecedor.setId(1);
fornecedor.setName("Bruno");
fornecedor.setEmail("bruno.camargo_@outlook.com");
fornecedor.setComment("OK");

return fornecedor;
}
}


But when I do this, I got the following error:

The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException


Why is this happening? Thanks in advance

Answer

you need to create icrud object to pass

try this

public interface ICrud {
    public Fornecedor getAllFornecedores();
}

public class Crud implements ICrud{ 
    public Fornecedor getAllFornecedores(){
        Fornecedor fornecedor = new Fornecedor();
        fornecedor.setId(1);
        fornecedor.setName("Bruno");
        fornecedor.setEmail("bruno.camargo_@outlook.com");
        fornecedor.setComment("OK");

        return fornecedor;
    }
}

public class Controller { 


private ICrud crud = new Crud();


@GET
@Path("/getallfornecedores")
@Produces("application/json;")
public Fornecedor getAllFornecedores(){
    return crud.getAllFornecedores();
}

}
Comments