web services - WebService Project Java on Gradle Project -
i'm developing webservice in java using netbeans , using gradle depency mannagement.
i found this article developing web projects gradle. uses gretty plugin. followed instructions ( changed servlet container jetty tomcat ) , develop/deploy web project , open "home page" servlet.
the problem webservice classes not working properly. requests made browser returns 404 error code. testing, made new webproject using netbeans time without gradle , works charm.
here follows 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() { // 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 url get: http://localhost:8088/sisvendas/produtos
as said before, following request works: http://localhost:8088/sisvendas/ ( there index.html file useless )
what i'm doing wrong?
jax-rs specification. needs implemented have use. dependency have javax.ws.rs-api
, specification jar. there no implementation, meaning there no engine run application. implementation provide engine run jax-rs application.
that being said, jax-rs specification part of java ee specification, if running in java ee compliant application server, wildfly or glassfish, application server have jax-rs implementation internally, need @ application project level, specification jar compile source code, , application server have engine run application @ runtime.
but in case, tomcat is not java ee compliant server. servlet container, implements servlet specification. still need jax-rs implementation if want use jax-rs tomcat. jersey 1 such implementation. use jersey implementation, need 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 add json/pojo support. thing missing in code @applicationpath
annotation on application
class
@applicationpath("/") public class apllicationconfig extends application {
the @applicationpath
annotation sets servlet mapping jersey application. if changed /api
, uri access is
http://localhost:8080/sisvendas/api/produtos