quinta-feira, 24 de abril de 2014

Criar endpoints com Google Cloud Endpoints

Após criado o projeto, criar a classe do endpoint com as anotações conforme abaixo. Onde:
@Api(name = "exampleProject") é o nome da aplicação
@ApiMethod(name =  "customers.list",, httpMethod = HttpMethod.GET) - é o nome do serviço


@Api(name = "exampleProject")
public class CustomerEndpoint {

private static final String ENTIDADE = "Cliente";

@ApiMethod(name =  "customers.list",, httpMethod = HttpMethod.GET)
public CollectionResponse<Customer> listCustomer() throws Exception {
try {
List<Customer> list = Customer.getDao().list();
return CollectionResponse.<Customer> builder().setItems(list).build();
} catch (Exception e) {
throw new Exception(String.format(Message.ERRO_LIST, ENTIDADE));
}
}

@ApiMethod(name = "customers.get", httpMethod = HttpMethod.GET)
public Customer getCustomer(@Named("id") Long id) throws Exception {
try {
return Customer.getDao().get(id);
} catch (Exception e) {
throw new Exception(String.format(Message.ERRO_GET, ENTIDADE));
}
}

@ApiMethod(name = "customers.insert", httpMethod = HttpMethod.POST)
public Customer insertCustomer(Customer model) throws Exception {
try {
return Customer.getDao().insert(model);
} catch (Exception e) {
throw new Exception(String.format(Message.ERRO_INSERT, ENTIDADE));
}
}

@ApiMethod(name = "customers.update", httpMethod = HttpMethod.PUT)
public Customer updateCustomer(Customer model) throws Exception {
try {
return Customer.getDao().update(model);
} catch (Exception e) {
throw new Exception(String.format(Message.ERRO_UPDATE, ENTIDADE));
}
}

@ApiMethod(name = "customers.remove", httpMethod = HttpMethod.DELETE)
public void removeCustomer(@Named("id") Long id) throws Exception {
try {
Customer.getDao().delete(id);
} catch (Exception e) {
throw new Exception(String.format(Message.ERRO_REMOVE, ENTIDADE));
}
}

Nenhum comentário: