How to implement jaxrs application without web.xml

I am trying to deploy a really simple jaxrs application without the web.xml configuration and cannot make it work. My url I would expect to get is serverandport / {appname} / rest / greetings / hello and I think I should be missing out on something dead.

application

@ApplicationPath("/rest") public class EngineApp extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> s = new HashSet<Class<?>>(); s.add(RestTestImpl.class); return s; } } 

Resource

 @Path("/welcomes") public class RestTestImpl { @GET @Path("hello") public String sayPlainHello() { return "Hi from Rest"; } } 

POM fragment

 <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> 

Edit: in addition to the answer below, I tried with empty web.xml as well as with the following web.xml. Bother also returns 404, however the xml below says "Servlet javax.ws.rs.core.Application is not available":

web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" 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_2_5.xsd" > <servlet> <servlet-name>javax.ws.rs.core.Application</servlet-name> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>javax.ws.rs.core.Application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> 
+4
source share
3 answers

You need to deploy the application to a container compatible with Servlet 3.0 in order to use this functionality. Try GlassFish 3.x or Tomcat 7 .

+3
source

I found a pretty similar question, and the problem found there may help you. I would definitely try this with empty web.xml with version 3.0 of the schema to see if it helps.

JBoss AS 7 Failover Web Services Not for Auto Deployment

0
source

For those who want to use the Servlet 2.0 specification, create web.xml and add the maven servlet dependency:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" 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"> <servlet> <servlet-name>REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.mycompany.rest.engine.EngineApp</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> 

Add pom dependency

 <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.13</version> </dependency> 
-3
source

Source: https://habr.com/ru/post/1445958/


All Articles