Skip to content

Instantly share code, notes, and snippets.

@murilogteixeira
Last active May 25, 2020 17:23
Show Gist options
  • Save murilogteixeira/9578d4f078d015b2197035c73dca15cf to your computer and use it in GitHub Desktop.
Save murilogteixeira/9578d4f078d015b2197035c73dca15cf to your computer and use it in GitHub Desktop.
Create Maven Project using Jersey in Eclipse on Mac

Step 1

Crie um Dynamic Web Project no Eclipse

Step 2

Converta-o para um Maven Project

Step 3

Crie o arquivo web.xml

Step 4

No arquivo pom.xml, adicione o seguinte código abaixo de </build>

	<dependencies>
		<dependency>
			<groupId>asm</groupId>
			<artifactId>asm</artifactId>
			<version>3.3.1</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-bundle</artifactId>
			<version>1.19.4</version>
		</dependency>
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20170516</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.19.4</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-core</artifactId>
			<version>1.19.4</version>
		</dependency>
	</dependencies>

Step 5

No arquivo web.xml, adicione o seguinte código acima de </web-app>:

	<servlet>
		<servlet-name>Jersey Web Application</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey Web Application</servlet-name>
		<url-pattern>/api/*</url-pattern>
	</servlet-mapping>

Step 6

Para testar, crie uma classe com o nome Hello em JavaResources/src e substitua o arquivo por:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.json.JSONObject;

@Path("/teste")
public class Hello {
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public String get() {
		JSONObject json = new JSONObject();
		json.put("teste", "testando");
		return json.toString();
	}
}

Step 7

Agora execute o projeto e no navegador faça uma requisição com o seguinte endereço: http://localhost:8080/zHello2/api/teste

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment