java - basic Jersey webservice with maven jetty plugin -
i have been working restfull webservices provide jersey in combination maven jetty plugin @ previous job. personal project starting scratch myself , cant seem basic version going. have tried follow few tutorial either old or off topic.
the recent example tried was: jersey 2 + spring 4 + jetty-maven-plugin
so below configs make plug in run no matter url try gives me 404's
pom:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <packaging>war</packaging> <groupid>foo</groupid> <artifactid>bar</artifactid> <version>1.0-snapshot</version> <properties> <jetty.version>9.3.11.v20160721</jetty.version> <jersey.version>2.23.1</jersey.version> </properties> <dependencies> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet</artifactid> <version>${jersey.version}</version> </dependency> <dependency> <groupid>org.glassfish.jersey.core</groupid> <artifactid>jersey-server</artifactid> <version>${jersey.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.eclipse.jetty</groupid> <artifactid>jetty-maven-plugin</artifactid> <version>${jetty.version}</version> </plugin> </plugins> </build> </project>
web.xml
<?xml version="1.0" encoding="iso-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>test</display-name> <servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>javax.ws.rs.application</param-name> <param-value>com.foo.bar</param-value> </init-param> <!--<load-on-startup>1</load-on-startup>--> </servlet> <servlet-mapping> <servlet-name>jersey rest service</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
resourceconfiguration.java
package com.foo.bar; import org.glassfish.jersey.server.resourceconfig; public class resourceconfiguration extends resourceconfig { public resourceconfiguration() { register(entrypoint.class); } }
and service class
package com.foo.bar; import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; public class entrypoint { @get @path("/test") @produces(mediatype.text_plain) public string test() { return "it works"; } @get @path("/") @produces(mediatype.text_html) public string test2() { return "it works"; } }
nothing fancy right feel should work out of box. jetty server starts said jetty 404 pages @ every url permutation try.
thanks in advance , input.
few changes need done make work. please follow below steps.
step 1: change web.xml below pojo mapping , resourse configuration class instead of package name.
<servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>javax.ws.rs.application</param-name> <param-value>bar.com.ravi.resourceconfiguration</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey rest service</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
step 2: change resourceconfiguration class below package information.
public class resourceconfiguration extends resourceconfig { public resourceconfiguration() { packages("bar.com.ravi"); register(entrypoint.class); } }
step 3: change entrypoint class class level @path annotation.
import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; @path("/hi") public class entrypoint { @get @path("/test") @produces(mediatype.text_plain) public string test() { return "it works"; } @get @path("/") @produces(mediatype.text_html) public string test2() { return "it works"; } }
step 4: change pom.xml in build tag below.
<build> <finalname>bar</finalname> <plugins> <plugin> <groupid>org.eclipse.jetty</groupid> <artifactid>jetty-maven-plugin</artifactid> <version>9.2.11.v20150529</version> <configuration> <scanintervalseconds>10</scanintervalseconds> <webapp> <contextpath>/bar</contextpath> </webapp> <httpconnector> <port>8888</port> </httpconnector> </configuration> </plugin> </plugins> </build>
step 5: run below command command promt.
mvn jetty:run
step 6: hit browser below url
http://localhost:8888/bar/hi/test
Comments
Post a Comment