Guerino Rodella Guerino Rodella - 6 months ago 59
Java Question

WebService Project Java on Gradle Project

I'm developing a WebService in Java using NetBeans and using gradle as depency mannagement.

I found this article about developing web projects with gradle. It uses the Gretty plugin. I followed the instructions ( just changed the servlet container from jetty to tomcat ) and could develop/deploy a web project and open the "home page" from the servlet.

The problem is my WebService classes are not working properly. All GET requests made from my browser returns 404 error code. For testing, I made a new WebProject using Netbeans but this time without Gradle and it works like a charm.

Here follows the code:

build.gradle

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.akhikhl.gretty:gretty:+'
}
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'

gretty {
port = 8088
contextPath = '/sisvendas'
servletContainer = 'tomcat8'
}
repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0.1'

}


The Resource:

@Path("produtos")
public class ProdutosResource {

@Context
private UriInfo context;

public ProdutosResource() {
}

@GET
@Produces("application/json")
public String getJson() {
// Just for testing...
return "{\"produtos\":\"arroz\"}";
}

@PUT
@Consumes("application/json")
public void putJson(String content) {
}
}


Application config class:

public class ApllicationConfig extends Application{

@Override
public Set<Class<?>> getClasses() {

Set<Class<?>> resourcesSet = new java.util.HashSet<>();
adicionarClassesRecursos( resourcesSet );
return resourcesSet;

}

private void adicionarClassesRecursos( Set<Class<?>> resources ) {
resources.add( com.gear.dev.webprojectgradle.resources.ProdutosResource.class );
}

}


And this is the url for GET: http://localhost:8088/sisvendas/produtos

As I said before, the following request works: http://localhost:8088/sisvendas/
( There is also a index.html file which is useless )

What i'm doing wrong?

Answer

JAX-RS is only a specification. It needs to be implemented to have any use. The dependency you have javax.ws.rs-api, is only the specification jar. There is no implementation, meaning there is no engine to run the application. It is up the implementation to provide an engine to run a JAX-RS application.

That being said, the JAX-RS specification is a part of the Java EE specification, so if you are running in a Java EE fully compliant application server, like Wildfly or Glassfish, then that application server will have the JAX-RS implementation already internally, so all you would need at the application project level, is the specification jar to compile your source code, and the application server will have the engine to run the application at runtime.

But in your case, Tomcat is not a Java EE compliant server. It is only a servlet container, that implements the servlet specification. So you still need a JAX-RS implementation if you want to use JAX-RS with Tomcat. Jersey is one such implementation. To use the Jersey implementation, you really only need the following dependency

compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.23`
compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.23`

The latter dependency is to add JSON/POJO support. Another thing you are missing in your code is an @ApplicationPath annotation on your Application class

@ApplicationPath("/")
public class ApllicationConfig extends Application {

The @ApplicationPath annotation sets the servlet mapping for the Jersey application. If you changed it to /api, then the URI you would access is

http://localhost:8080/sisvendas/api/produtos