Tomcat Couldn't find Webservice 404 - java

I am creating a simple webservice project that pulls data from db on countries and displays it in a JSON form, My project does not throw any error and builds successfully but it is unable to find deployed resource.
WorldInformation.java class
package com.webservices;
import java.util.List;
import com.webservices.models.Country;
import com.webservices.services.WorldInformationService;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
#Path("/worldinformation")
public class WorldInformation {
WorldInformationService worldInformationService = new WorldInformationService();
#GET
#Path("/getCountries")
#Produces(MediaType.APPLICATION_JSON)
public List<Country> getCountries(){
System.out.println("reached point 1");
List<Country> countryList = worldInformationService.getCountries();
return countryList;
}
#POST
#Path("/setCountry/{country}/{countryCode}")
#Consumes(MediaType.TEXT_PLAIN)
public void setCountry(#PathParam("country") String country,#PathParam("countryCode") String countryCode){
worldInformationService.setCountry(country,countryCode);
}
}
This is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And this is what pom.xml looks like
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webservices</groupId>
<artifactId>WorldInformation</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WorldInformation</name>
<build>
<finalName>WorldInformation</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
This is error snapshot
I can't seem to figure out how to get this resolved and what's going wrong, will deeply appreciate your help on this.

I think you need to add #ApplicationPath in your jax-rs application as well.
Note: The URL is also missing the context path.
For example: http://localhost:8080/<context-path>/servlet/path
From Oracle Docs :
The #ApplicationPath annotation is used to define the URL mapping
for the total application. The path specified by #ApplicationPath is the
base URI for all resource URIs specified by #Path annotations in the
resource class.
For example : Add a class WorldInformationApp annotated with #ApplicationPath in the project.
#ApplicationPath("/worldinfo")
public class WorldInformationApp extends Application {
}
Try to access the resource with this url : http://localhost:8080/<context-path>/worldinfo/worldinformation/getCountries
Note : I think you are using wrong libraries for jax-rs app.
For example : javax.ws.rs.* should be used instead of jakarta.ws.rs.*

Thanks for your valuable feedbacks, here is what was worked for me. As someone suggested. I was not looking at correct point. I was missing application name in the url I was trying to connect to. Though I ran into other problems but I managed to solve them and now its working for me.
localhost:8080/WorldInformation/worldinformation/getCountries
Thanks for your valuable help on this.

Related

Swagger - Jersey - Tomcat (Not able to generate swagger.json, shows 404 : Not found)

I am new to swagger and jersey.
My api operations are working fine. I followed the steps in the link: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 to add swagger into my api. When I give my try to access the swagge.json via the url:http://localhost:8080/messenger/webapi/swagger.json I get 404 -Not found. Here are my api codes. Can you please help me out with this.
pom.xml file
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vishwas</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
io.swagger.jaxrs.listing,
com.vishwas.messenger
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Swagger Configuration</servlet-name>
<servlet-class>com.vishwas.messenger.sevlet.SwaggerConfigurationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/messenger/webapi</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
3.Beans config class for swagger
package com.vishwas.messenger.sevlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import io.swagger.jaxrs.config.BeanConfig;
public class SwaggerConfigurationServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setTitle("Messenger Api documentation");
beanConfig.setBasePath("/messenger/webapi");
beanConfig.setResourcePackage("com.vishwas.messenger.resources");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
[output screenshot][1]
[1]: https://i.stack.imgur.com/fLx83.png
I found the solution to the problem, I changed the swagger-jersey2-jaxrs artifact version to the latest one and also installed the compile dependencies along with it which was provided in the maven repository website.
Please check the version properly!!

Unable to run JaxRs web service using Wildfly web server

Hi I am trying to build a Simple JaxRs web service on JBoss developer studio and Wildfly 11 as application server but i am getting following error while i am trying to deploy my maven project :
Failed to start service
jboss.undertow.deployment.default-server.default-host."/JaxRsTest-0.0.1-SNAPSHOT":
org.jboss.msc.service.StartException in service
jboss.undertow.deployment.default-server.default-host."/JaxRsTest-0.0.1-SNAPSHOT":
java.lang.NoSuchMethodError:
org.apache.tomcat.util.descriptor.DigesterFactory.newDigester(ZZLorg/apache/tomcat/util/digester/RuleSet;Z)Lorg/apache/tomcat/util/digester/Digester;
Also i want to inform you that the project is working fine when i use apache tomcat 9 However it throws above mentioned error when I switch to wildfly 11. I am new to java and Specially web service and just started working for a company which uses Jboss eap server which is quite same as wildfly and unfortunately i am receiving same error there as well while working on a web service assignment.
For more reference below is my pom.xml file :
<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>
<groupId>JaxRsServiceTest</groupId>
<artifactId>JaxRsServiceTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
and my web.xml file content is :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JaxRsServiceTest</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.jaxrs.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
and my java class within package com.test.jaxrs.service is
package com.test.jaxrs.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/test")
public class JaxRsTest {
#GET
#Path("/hello/{msg}")
#Produces(MediaType.TEXT_PLAIN)
public String processRequest(#PathParam(value="msg")String message)
{
return "Hello : " + message;
}
}
Please let me know where i am going wrong with the wildfly.... and Thanks in advance
Wildfly and EAP are a full JEE servers - there is no need to include Jersey dependencies in your pom.xml. Tomcat required that because it is primarily a servlet/JSP engine. Your service looks good but your pom.xml and web.xml are the result using Tomcat. Additionally, you do not need a web.xml any longer if you don't want and you certainly don't need to use Jersey to map it.
You'll want one more file - the Application class that gets things started. It looks something like:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* Used to bootstrap JAX-RS. Otherwise this class is
* not directly used.
*/
#ApplicationPath("/rest")
public class RestApplicationConfig extends Application {
// intentionally empty
}
It can go anywhere in your source tree. Note that it takes over the /rest path which is similar to what you had in your web.xml.
The pom.xml below will create a .war file that you can deploy to Wildfly. This is about as simple as it gets and again, remove your web.xml for now.
pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JaxRsServiceTest</groupId>
<artifactId>JaxRsServiceTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.12.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

Maven + Jersey RESTful API : Internal Server Error from POSTMAN on GET request

I am sending a GET request to my StoryBoardResource, class responsible for generating JSON format data, using POSTMAN app. The associated method consumes nothing but produces Application/JSON data: #Produces(MediaType.APPLICATION_JSON).
I am recieving 500 internal server error on POSTMAN though there is nothing showing up on my IDE's console as am logging some statements too for debugging purpose.
If there had been some jar file issue it must have thrown an error for no MesageBodyWriter found ...right? I have updated pom.xml to include JSON dependency and the jar is also there.
Here is my StoryBoardResource class to which the request is delegated:
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.harsh.webapp.storyBoard.model.StoryBoardModel;
import org.harsh.webapp.storyBoard.service.StoryBoardService;
#Path("/authenticateUser")
public class StoryBoardResource {
private StoryBoardService sts = new StoryBoardService();
#GET
#Produces(MediaType.APPLICATION_JSON)
public StoryBoardModel authUser(#QueryParam("username") String username, #QueryParam("password") String password){
return sts.authUser(username, password);
}
}
which calls authUser method on my service class StoryBoardService :
public StoryBoardModel authUser(String username, String password){
System.out.println("In here");
if(username.equals(map.get(username).getUsername()) && password.equals(map.get(username).getPassword())){
System.out.println("true");
return map.get(username);
}
return map.get(username);
}
Could this be bcoz of some jar or Jersey version conflict?
Here is my pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.harsh.webapp</groupId>
<artifactId>storyBoard</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>storyBoard</name>
<build>
<finalName>storyBoard</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
And here is a snapshot of Maven Dependencies
Sorry for such a long post but I thought I should explain things clearly.
Please help on finding what could I be doing wrong here?
And web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.harsh.webapp.storyBoard</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
I worked it out and it appeared so that I missed adding a NO-Argumentconstructor in a model class.
public someClass(){};
Always remember to add a No-Arg constructor whenever you create a model class for your Maven-Jersey application.
And it worked!
However, I am not clear on why would a no-arg constructor make the application run or fail!!
If any body know the answer to this. Please share your knowledge!!

jersey 2 + spring 4 + jetty-maven-plugin

I am trying to make an example of using jersey 2 + spring 4 + jetty-maven-plugin. But keep getting this error, cannot understand why.. Please give me a hand.
WARNING: The Jersey servlet application, named com.joejag.code.orders.restservices.ResourceConfiguration, is not annotated with ApplicationPath and has no servlet mapping.
2015-12-16 19:56:38.746:INFO:/.0-SNAPSHOT:main: Spring WebApplicationInitializers detected on classpath: [org.glassfish.jersey.server.spring.SpringWebApplicationInitializer#2776015d]
2015-12-16 19:56:38.778:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext#15fb7a32{/orders-server-1.0-SNAPSHOT,file:///home/bryan-1/workspace/project/java/simple-java-restful-service-using-jersey-and-maven-master/src/main/webapp/,STARTING}{file:///home/bryan-1/workspace/project/java/simple-java-restful-service-using-jersey-and-maven-master/src/main/webapp/}
java.lang.IllegalStateException: No such servlet: Example
My POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.joejag.code.orders</groupId>
<artifactId>orders-server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Example</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<jersey.version>2.22.1</jersey.version>
</properties>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>https://maven.java.net/content/groups/public/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Jersey 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>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring4</artifactId>
<version>3.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.6.v20151106</version>
<configuration>
<scanTargets>
<scanTarget>${project.basedir}/src/main</scanTarget>
<scanTarget>${project.basedir}/src/test</scanTarget>
</scanTargets>
<webAppConfig>
<contextPath>/${project.artifactId}-${project.version}</contextPath>
</webAppConfig>
<contextPath>${project.artifactId}</contextPath>
</configuration>
</plugin>
</plugins>
</build>
</project>
My web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Example</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.joejag.code.orders.restservices.ResourceConfiguration</param-value>
</init-param>
<!-- <init-param> <param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.joejag.code.orders.restservices</param-value> </init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My ResourceConfig class
package com.joejag.code.orders.restservices;
import org.glassfish.jersey.server.ResourceConfig;
public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
register(OrdersService.class);
}
}
My applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.joejag.code.orders.restservices" />
<bean id="transactionBo" class="TransactionBoImpl" />
<!-- <bean class="OrderService" /> -->
</beans>
EDITED
after fixing inconsistant servlet-name issue Jetty is able to load the servlet, but a new issue arise that all the #PUT method seems to no longer execute. Here the OrderService look like.
package com.joejag.code.orders.restservices;
import java.util.Map;
import java.util.TreeMap;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
// * curl -X PUT http://127.0.0.1:8080/orders-server/orders/1?customer_name=bob
// * curl -X GET http://127.0.0.1:8080/orders-server/orders/1
* curl -X GET http://127.0.0.1:8080/orders-server/orders/list
*/
#Component
#Path("orders")
public class OrdersService
{
public static Map<String, String> orders = new TreeMap<String, String>();
//#Autowired
//private TransactionBo transactionBo;
public OrdersService()
{
//his.transactionBo = transactionBo;
}
#Path("/{order}")
#PUT
#Produces("text/html")
public String create(#PathParam("order") String order, #QueryParam("customer_name") String customerName)
{
orders.put(order, customerName);
return "Added order #" + order ;//+ this.transaction.save();
}
#Path("/{order}")
#GET
#Produces("text/html")
public String find(#PathParam("order") String order)
{
if (orders.containsKey(order))
return "<h2>Details on Order #" + order + "</h2><p>Customer name: " + orders.get(order);
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
#Path("/list")
#GET
#Produces("text/html")
public String list()
{
String header = "<h2>All Orders</h2>\n";
header += "<ul>";
for (Map.Entry<String, String> order : orders.entrySet())
header += "\n<li>#" + order.getKey() + " for " + order.getValue() + "</li>";
header += "\n</ul>";
return header;
}
}
servlet-name in the web.xml has to be consistant. After fixing that jetty is able to load the servlet.

Different behaviour using mvn jetty:run or jetty standalone

I have a project with multiple modules.
Two of them generate war files.
One war file is a REST application and provides a couple of resources.
The Other war file is a Angular JS web application (static content only) to talk to the REST backend.
For demo purposes I'd like to deploy both war-files very easily with mvn jetty:run
For development purposes I'd like to deploy them from my IDE (e.g. Eclipse Servers View).
When I do the deployment on a single Jetty Server (v9.0.7.v20131107) manually by starting the server and copiing the war-files to the deployment folder everything comes up.
When starting the jetty by mvn jetty:run both war files get deployed, but somehow the REST Resources do not get deployed.
I am using Jersey 2. When deploying manually I get a log message like
Nov 14, 2013 10:44:37 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4 2013-10-24 18:25:49...
However this message is not been shown when starting with mvn jetty:run. Therefore I assume that Jersey does not kick in.
For Dependency-Injection I use spring.
This is the parent pom in /pom.xml with the jetty-maven-plugin configuration
<project ...>
...
<build>
<plugins>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module1/target/module1-${project-version}.war</war>
<contextPath>/module1</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module2/target/module2-${project.version}.war</war>
<contextPath>/module2/contextPath>
</contextHandler>
</contextHandlers>
</configuration>
</plugins>
</build>
...
</project>
This is the pom for module1 (the REST module)
<parent>
...
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<springVersion>3.1.4.RELEASE</springversion>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4</version>
</dependency>
<!-- Dependencies to internal modules -->
...
<!-- Depenencies to internal modules END -->
</dependencies>
</project>
This is the web.xml for module1
<web-app ...
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
This is the applicationContext.xml for module1
<beans ...>
<context:annotation-config/>
<context:component-scan base-package="com.stackoverflow.zip"/>
<aop:aspectj-autoproxy/>
</beans>
This is the Module1Application class for module1
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
#ApplicationPath("/rest")
public class Module1Application extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Resource1.class);
classes.add(Resource2.class);
classes.add(MultiPartFeature.class);
return classes;
}
}
This is the pom for module2 (the AngularJS app)
Very light :)
<project ...>
<parent>
...
</parent>
...
<modelVersion>4.0.0</modelVersion>
<artifactId>module2</artifactId>
<packaging>war</packaging>
</project>
Do you have any idea why the Jersey Application does not get instantiated while running with mvn jetty:run but when running it manually?
I appreciate any input on this topic.
Kind regards
- zip
Your javax.servlet version is 2.5 in pom.xml, but 3.0 in web.xml. Try upgrading it to 3.0 in pom.xml. Is it possible that the IDE provides a servlet 3.0 for you?
Also, since Jetty provides a javax.servlet container, try putting the Servlet Api into provided scope in pom.xml. Something like this:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<!-- http://stackoverflow.com/a/15601606 http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope -->
<!-- This allows us to compile the application locally but does not include the jar in the package step -->
<scope>provided</scope>
</dependency>
It's possible that you want to add a <load-on-startup>1</load-on-startup> element to your web.xml. Check out http://tutorials.jenkov.com/java-servlets/web-xml.html#load-on-startup
That's whole tutorial is pretty good.
Disclaimer: I'm not familiar with what the Spring dependencies are doing to help your application bootstrap itself. You can always try removing dependencies on a separate branch, and adding them back in as you get things running
I haven't had a chance to play with Jersey 2.x so far, but from what I see in your web.xml, it doesn't look like you have Jersey setup correctly.
In a non-Spring application, you would have something like the following:
<web-app ...>
...
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.foo.rest</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>
In a Spring-based one, you would use:
<web-app ...>
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.foo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
Furthermore, check that you have these Maven dependencies:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

Categories