I have a very simple java web application where I use maven for build process and try deploying it on Tomcat.
I am using tomcat 7.0.5332, maven version 2.2.1.
Here is my very simple web app:
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.tugay.anotherwebapp</groupId>
<artifactId>another-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>another-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
<build>
<finalName>another-webapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
And here is my web.xml:
<web-app 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_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>MyServletName</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServletName</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
</web-app>
And MyServlet class:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* User: Tugay
* Date: 4/5/14
* Time: 9:14 PM
*/
public class MyServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws IOException {
System.out.println("Hello World!");
}
}
So
mvn clean install
works fine and the war is generated. But when deployed on Tomcat I get this:
- jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: /javax/servlet/Servlet.class
What is it that I am doing wrong?
I think you should set this dependency to provided:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Because it will already be in the tomcat shared classloader
If you look at the Servlet Spec 3.0, section 10.7.2 - you will find that the problem seems to be that you are trying to load Servlet class that is already provided by the container (Tomcat). Please, check whether servlet-api is in your war(it should not be) and use provided scope for javaee-api.
It's normal. Servlet containers provide their own servlet-api implementation classes. So classes included in your war (from javaee-api) clash with ones provided by tomcat.
To avoid this, set the scope of the javaee-api dependency to provided.
This way classes provided by that dependency will be visible at compile time but the jar itself won't be included in the resulting war.
Related
I am trying to run a simple app in Jakarta 9 that has a simple REST web service that returns hello world. However, when I try to access the web service using the localhost:8080/<context_root>/<application_path>/<resource_path I get 404. When I just target localhost:8080/<context_root> everything runs fine (I have an index.html in the project).
Here is some additional info. I am running the app packaged as a WAR inside Wildfly 25 running inside the Docker container.
Here's the relevant file content
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tyuiop32</groupId>
<artifactId>TimeTracking</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>9.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.23.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>TimeTracking</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
ApplicationConfig.java:
package com.poortoys;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
/**
* Register Jakarta REST resources.
*
*/
#ApplicationPath("")
public class ApplicationConfig extends Application {
}
HelloResource.java:
package com.poortoys.examples;
import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
/**
* Sample JAX-RS resources.
*
*/
#Path("test")
#RequestScoped
public class HelloResource {
#GET
#Path("hello")
#Produces(MediaType.APPLICATION_JSON)
public String getMessage() {
return "Hello, world";
}
}
When I try to GET http://localhost:8080/TimeTracking/test/hello I get 404
Targeting just http://localhost:8080/TimeTracking returns the content of the index.html page that is placed inside the webapp folder
PS: I went trough all the relevant threads and didn't find the asnwer
You'd need to use WildFly 27. WildFly 25 is a Jakarta EE 8 container while WildFly 27 is a Jakarta EE 10 container. You could use WildFly 25 Preview, but I would suggest using WildFly 27.
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.
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>
I started to play with JavaEE stuff, especially RESTEasy and I try to put together an small service packaged by maven. The service is done, ear package is created and deployed to a WildFly 10 server. No errors, but I'm not able to reach/call the service method.
I went through a lot of questions and answers here and according to them my app should work and I should be able to call it. One thing I don't know and haven't found any documentation so far how the url to reach the application is created.
The main question here: what are the rules of this url creation? Where can I find it?
So far, I have seen the following parameters can affect the url:
war file name (which one, the original (ends with SNAPSHOT string, or the finalName parameter defined in the rest project's pom.xml or the bundleFileName in ear package's pom.xml? Does thi war file name matters if war file is packaged into an ear?)
context root value defined in pom.xml of ear project (it is copied to application.xml file)
#ApplicationPath annotation over the Application class of RestEasy
web.xml if there is any
#Path values over the service classes and methods
Let's see my application.
My app consist of 3 maven module. There is the main module (pom module) having two modules, a, ear package module, b, rest api module creates war file. Pom.xml files are below.
Maven modules:
The pom module doesn't do anything.
The ear module puts together the ear file, and importantly it renames the war file to MasterData.Rest.Api.war.
The rest api web module produces a war file.
Eventually, the ear file looks like this:
Masterdata.Dataservice.ear
Masterdata.Dataservice.ear/MasterData.Rest.Api.war
No web.xml file, and no jboss.xml file.
According to a few articles and example the Currency/Currencies method should be available using the following urls (neither of them working):
http://localhost:8080/Currency/Currencies
or http://localhost:8080/MasterData.Rest.Api.war/Currency/Currencies
Just simply deploying the war file doesn't work too. By default the file has this ugly name: digitallibrary.dataservice.masterdata.rest.api-1.0-SNAPSHOT.war. After successful deployment the endpoint should be available here:
http://localhost:8080/digitallibrary.dataservice.masterdata.rest.api-1.0-SNAPSHOT.war/Currency/Currencies
It doesn't work.
Wildfly says the following and it can mean that the application can be accessed via the following urls (I'm just guessing...), but neither of them working...
http://localhost:8080/Currency/Currencies
http://localhost:8080/MasterData.Dataservice.ear/Currency/Currencies (it doesn't have any point in my opinion)
http://localhost:8080/MasterData.Dataservice.ear/MasterData.Rest.Api.war/Currency/Currencies
http://localhost:8080/MasterData.Rest.Api.war/Currency/Currencies
Wildfly's deployment log.
19:58:17,673 INFO [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of "MasterData.Dataservice.ear" (runtime-name: "MasterData.Dataservice.ear")
19:58:17,682 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0207: Starting subdeployment (runtime-name: "MasterData.Rest.Api.war")
19:58:17,712 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 111) WFLYUT0021: Registered web context: /
19:58:17,727 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "MasterData.Dataservice.ear" (runtime-name : "MasterData.Dataservice.ear")
Server is running and no errors.
package app;
import endpoints.CurrencyEndpoint;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
/**
* Created by SayusiAndo on 6/9/2017.
*/
public class MasterDataRestEndpointApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public MasterDataRestEndpointApplication() {
singletons.add(new CurrencyEndpoint());
}
#Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
return set;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
package endpoints;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
/**
* Created by SayusiAndo on 6/9/2017.
*/
#Path("/currency")
public class CurrencyEndpoint {
#GET
#Path("/currencies")
#Produces(MediaType.APPLICATION_JSON)
public Response getCurrencies() {
ArrayList list = new ArrayList<String>();
list.add("currency1");
list.add("currency2");
return Response
.status(Response.Status.OK)
.entity(list)
.build();
}
}
POM module 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sayusiando</groupId>
<artifactId>digitallibrary.dataservice.masterdata</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>digitallibrary.dataservice.masterdata.rest.api</module>
<module>digitallibrary.dataservice.masterdata.package.ear</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.2.0.Alpha4</version>
</plugin>
</plugins>
</build>
</project>
EAR module 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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>digitallibrary.dataservice.masterdata</artifactId>
<groupId>com.sayusiando</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>ear</packaging>
<artifactId>digitallibrary.dataservice.masterdata.package.ear</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<finalName>MasterData.Dataservice</finalName>
<modules>
<webModule>
<groupId>com.sayusiando</groupId>
<artifactId>digitallibrary.dataservice.masterdata.rest.api</artifactId>
<bundleFileName>MasterData.Rest.Api.war</bundleFileName>
<contextRoot>/</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sayusiando</groupId>
<artifactId>digitallibrary.dataservice.masterdata.rest.api</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
</project>
REST module 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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>digitallibrary.dataservice.masterdata</artifactId>
<groupId>com.sayusiando</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<artifactId>digitallibrary.dataservice.masterdata.rest.api</artifactId>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m07</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.1.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.1.2.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Generated application.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
"http://java.sun.com/dtd/application_1_3.dtd">
<application>
<display-name>digitallibrary.dataservice.masterdata.package.ear</display-name>
<module>
<web>
<web-uri>MasterData.Rest.Api.war</web-uri>
<context-root>/</context-root>
</web>
</module>
</application>
I made a few mistakes, but I learned a few things.
First mistake:
For some reason I removed the #ApplicationPath() annotation over the RestEasy application path. Not having this and web.xml Wildfly didn't know what to do these classes. So they were not registered.
Putting back the #ApplicationPath() annotation, and removing the web.xml made the situation better.
Second mistake:
For some reason ( :) ), I checked the http://localhost:8000 instead of http://localhost:8080. The latter is WildFly's site. The first one is nothing.
Answering my questions:
Name of EAR and WAR files do not matter in this case
The url is created according to the following: http://{host}:{port}/{applicationPathvalue}/{pathValueOfClass}/{pathValueOfMethod}
In my case:
{host}: localhost
{port}: 8080
{applicationPathValue}: api (check the code sample below)
{pathValueOfClass}: currency (check the code in my question)
{pathValueOfMethod}: currencies (check the code in my question)
#ApplicationPath("/api")
public class MasterDataRestEndpointApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public MasterDataRestEndpointApplication() {
singletons.add(new CurrencyEndpoint());
}
#Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
return set;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
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!!