Bruno Camargo
Bruno Camargo

Reputation: 19

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("[email protected]");
        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

Upvotes: 1

Views: 116

Answers (2)

Mithat Konuk
Mithat Konuk

Reputation: 457

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("[email protected]");
        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();
}

}

Upvotes: 1

Bruno Camargo
Bruno Camargo

Reputation: 19

Thank you mithat konuk. The resolution was instantiate the interface with the implementation.

Upvotes: 0

Related Questions