quinta-feira, 24 de abril de 2014

Testando Endpoints

Após criado a classe do endpoint, para que ela seja acessível, deve ser mapeada no web.xml, conforme abaixo:

        <servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>
br.com.super2.endpoint.ProjectEndpoint,
br.com.super2.endpoint.TaskEndpoint,
br.com.super2.endpoint.DiscussionEndpoint
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>


Ao executar o projeto, estar disponível a seguinte url: localhost:8888/_ah/api/explorer
Onde sera possível realizar os testes de todos os métodos do endpoint.

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

Criar projeto Maven + Google Cloud Endpoints

O projeto pode ser criado via archetypes, conforme o link: appengine + maven

Após criado, as dependencias ficam como abaixo:

<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.target.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.target.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-endpoints</artifactId>
<version>${appengine.target.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>