I'm following this guide to build my hello world web service and get kind of stuck, here are my codes:
MyResource.java:
package com.example;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("myresource")
public class MyResource {
#GET
#Produces("text/plain")
public String getIt() {
return "Got it!";
}
}
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>ts</groupId>
<artifactId>mtest</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>mtest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
<build>
<finalName>mtest</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.0.M1</version>
</plugin>
</plugins>
</build>
web.xml:
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- Servlet declaration can be omitted in which case
it would be automatically added by Jersey -->
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/myresource/*</url-pattern>
</servlet-mapping>
Now my problems are:
After launching Jetty, I can get a hello world of my jsp from http://localhost:8080/, but http://localhost:8080/myresource returns 404
Intellij complains Element web-app must be declared
Add your servlet declaration in the web.xml like this
<servlet>
<servlet-name>javax.ws.rs.core.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.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
that should work. And sometimes the path needs a bar like this.
#Path("/myresource")
And see that, if you put
<url-pattern>/myresource/*</url-pattern>
you have to hit
http://localhost:8080/myresource/myresource
one by your web configuration, and another by the path of your resource.
but, if you are trying this in tomcat, you need to include the artifactId too, like this:
http://localhost:8080/mtest/myresource/myresource
I hope that this help you.
Related
I have actually a similar problem as described here JAX-RS Resource not found in GlassFish Server. Since one should not ask for help in the answers I make a new question.
I created a project named "SPCore" which is a simple RESTful Webservice. It uses jax-rs libs, jdk 11 and a Tomcat 9.0.44 server. I have an index.html in the webapps folder which shows Hello World. The context path is set to /sp-core. If I'm calling localhost:8080/sp-core the index.html file is returned. So that works.
I have some resources (I will only show one as example). The resource "ProcessEventResource" should return "hello world" as plain text when a GET request comes. The path is set to #Path("processEvent"). Sadly I get an error 404 when I'm calling localhost:8080/sp-core/processEvent
I tried so far:
add an / at the beginning of the path declaration
I added a subclass application as mentioned here in the updated answer JAX-RS Resource not found in GlassFish Server. Still I get the 404 error when calling localhost:8080/sp-core/api/rest/processEvent
I have the assumption that something with the servlets or paths is not working correctly.
In the following I will provide my project structure, the pom file, the resource and the application class. I cut some small parts (package names, group-id, ...) which refers to the company I'm working for. If you need more Information please let me know.
Project structure
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>
<artifactId>sp-core</artifactId>
<version>1.0-SNAPSHOT</version>
<name>sp-core</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
processEventResource which simply returns hello world:
#Path("processEvent")
#Provider
public class ProcessEventResource {
public ProcessEventResource(){
}
#GET
#Produces("text/plain")
public String processEvent() {
return "Hello, World!";
}
}
RestApplication class:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/api/rest")
public class RestApplication extends Application {
}
I would expect to get the hello world response when I'm calling localhost:8080/sp-core/api/rest/processEvent
Thanks in advance for your help and time.
+++++++++++++++++++++++++++Update+++++++++++++++++++++++++++
I switched from dependency javax to jersey and added a web.xml file. The new pom.xml looks like this
<name>sp-core</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
The web.xml looks like this:
<display-name>sp-core</display-name>
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Now I either get a 404 with message "Not Found" or "The requested resource [/sp-core/api/rest/processEvent] is not available". Which path should go where so I get the return "Hello World" when I call localhost:8080/sp-core/api/rest/processEvent
+++++++++++++++++++++++++++Update+++++++++++++++++++++++++++
I added init parameter to the web.xml
<init-param>
<param-name>javax.ws.rs.core.Application</param-name>
<param-value>abc.de.fg.SPCore.RestApplication</param-value>
</init-param>
The error message is still the same.
After reading many stackoverflow questions, JAX-RS docu and in the end maven docu I finally got it done. So I will show what I have done in the end.
As I wrote in the first update I added a web.xml file which ended up looking like this.
<web-app version="3.0" 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">
<display-name>sp-core</display-name>
<servlet>
<servlet-name>restServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>abc.de.fg.sp_core.RestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restServlet</servlet-name>
<url-pattern>/api/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Please note, that the param name for the application is javax.ws.rs.Application and not javax.ws.rs.core.Application as said in the documentation (always read the notes ...).
I ended up only using the jersey container-servlet and the jersey-hk2 dependencies (JSON dependency is only for another resource). So the pom.xml looks like the following:
<?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>abc.de.fg</groupId>
<artifactId>sp-core</artifactId>
<version>1.0-SNAPSHOT</version>
<name>sp-core</name>
<packaging>war</packaging>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</project>
My RestApplication overrides the getClasses Method from the Application class. There I added all my resources. The ApplicationPath is set to /*
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
#ApplicationPath("/*")
public class RestApplication extends Application {
public RestApplication() {
}
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(ProcessEventResource.class);
return s;
}
}
And then it finally worked and I got my Hello World when requesting http://localhost:8080/sp-core/api/rest/processEvent.
Another short problem I run into was a 500 error missing RestApplication class. Simply forgot to maven compile ...
Hope this question and answer helps others.
Cheers :)
I've tried many different ways to make the resteasy client work for me, but I'm stuck on an error.
java.lang.LinkageError: Failed to link org/jboss/resteasy/client/jaxrs/internal/ClientConfiguration
Here is the method I'm using as a client
public void createIssue() {
issue = new Issue();
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("api-being-posted-to");
Response response = target.request().header("Authorization: ", "Basic Foo")
.post(Entity.entity(issueJson, "application/json"));
}
I can POST and GET with the same data this is using, and I've tried making a client work with different data and posting to different places and always get the same error. Here's my pom:
<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>org.foo.bar</groupId>
<artifactId>foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>example</name>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.8.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.1.0.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</build>
and here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
I'm using JBoss EAP 6.4 if that helps.
As far as I can tell the provided version of resteasy in JBoss EAP 6.4 is 3.0.21.Final and not 3.1.0.Final but you might have modified that (see https://developer.jboss.org/thread/262213 for reference).
You use different versions and different scopes for your resteasy libraries (3.0.8.Final for jaxrs-api) but that might still be ok.
I would always recommend to use the Resteasy Proxy Framework. It might be a bit more code to write but makes things easier and clearer in total:
Define an Interface:
public interface IssueIF
{
#POST
#Path("/issue")
#Produces("text/plain")
Issue postIssue(Issue i);
}
Use it:
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://example.com/base/uri");
SimpleClient simple = target.proxy(IssueIF.class);
Issue issue = new Issue();
client.postIssue(issue);
I did not compile this code, it is mostly copy/pasted from the docs but it should be close to what you need.
Edit:
From the docs:
Subclasses of LinkageError indicate that a class has some dependency
on another class; however, the latter class has incompatibly changed
after the compilation of the former class.
This suggest that you have a problem with different library versions you use for compilation and on runtime so see above at no 1.
Especially if you use different scopes it is essential that the versions are the same for compilation and runtime! So try to find out which versions your JBoss supplies and only use these versions. Also find out which of your referenced libraries are supplied and mark all of them with scope provided.
Condition
1.resteasy 3.0.4.Final
2.eclipse
3.maven
Action
I got war package into tomcat7(window x64) webapps and server run normally.
Visit http://localhost:8080/test-resteasy/rest/message/hello, and browser print "Restful example : hello".
But when I maven build tomcat7:run tomcat7-maven-plugin in eclipse,visit the same url throws:
javax.ws.rs.NotFoundException: Could not find resource for full path
Source (web.xml)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="3.0" 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">
<display-name>Archetype Created Web Application</display-name>
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</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>top.crowley.rest</groupId>
<artifactId>resteasy</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>resteasy Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</version>
</dependency>
</dependencies>
<build>
<finalName>test-resteasy</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8080</port>
<path>/test-resteasy</path>
<uriEncoding>UTF-8</uriEncoding>
<finalName>test resteasy</finalName>
<server>tomcat7</server>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
MessageRestService
package top.crowley.resteasy;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/message")
public class MessageRestService {
#GET
#Path("/{param}")
public Response printMessage(#PathParam("param") String msg) {
String result = "Restful example : " + msg;
return Response.status(200).entity(result).build();
}
}
Conjecture
I think of source code is correct, because of run war package in single tomcat 7 server successful. Maybe lack of some dependencies in pom.xml.
Thanks!
Really long post to analyze each POM dependency....but why don't you give a try to compile maven in this way, suggested by official resteasy team?
mvn clean install
mvn jetty:run
https://github.com/resteasy/resteasy-examples/tree/3.1.1.Final/resteasy-springMVC
Am new to JavaEE and have some issues getting custom ContainerRequestFilter to run in Jersey. I read the jersey documentation some more and created a new clean project straight from the 'jersey-quickstart-webapp', added the filter seen below but no luck (added an empty beans.xml as well).
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
#Provider
#PreMatching
public class MyFilter implements ContainerRequestFilter {
#Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
}
Was uncertain if Prematching and Provider was complementary or not so i used both then each separately, but didnt work (MyReasource just served as without filter). Tried throwing an exception in MyFilter but that didnt run either.
So i searched through StackOverflow and found 'http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/' which points to that you actually needs to implement registrations in Application or ResourceConfig class. I tried this (didnt work) but i atleast got a warning for the resource class now, 'No resource methods have been found for resource class a.b.MyFilter'
My Application class now looks like below (tried scan package but didnt make a difference. Without the manual filter registration i didnt get the warning either).
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import javax.ws.rs.ApplicationPath;
#ApplicationPath("resources")
public class RestApplication extends ResourceConfig {
public RestApplication() {
//packages("a.b");
register(MyFilter.class);
register(MyResource.class);
property(ServerProperties.TRACING, "ALL");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
</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>a.b</groupId>
<artifactId>server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>server</name>
<build>
<finalName>server</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.22.1</jersey.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
All files lies in the 'a.b' (i.e my package) root. Any ideas on how to get the filter actually running and i would be very greatful ;). I presume it shouldnt be this hard to get this working so i guess im missing something here?
Let me walk you through what's going on. When you first created the jersey-quickstart-webapp archetype, it gave you this
<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>a.b</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
What this init-param jersey.config.server.provider.packages does is tell Jersey what package(s) to scan for resource classes annotated with #Path, and provider classes annotated with #Provider.
So from that point, all you needed to do was add the #Provider to the filter, and it would have worked.
But then you decided to clear out the web.xml and use the ResourceConfig with the #ApplicationPath. For this to work Jersey takes advantage of the Servlet 3.0 pluggability mechanism, as mentioned in this answer. For that to work we need to make sure we have the jar that has the JerseyServletContainerInitializer. That's where we need to look at the pom.xml file
<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>
The comment is telling you that if you don't need Servlet 2.5 support, you should use jersey-container-servlet instead of jersey-container-servlet-core. I don't know, it might be poorly worded. Maybe instead it should say if you want Servlet 3.x support, change it. But in any case, the jersey-container-servlet has the JerseyContainerServletInitializer that we need. So if you want to go web.xml-less, then just switch out the dependency.
I am new to RestFul API. I have a sample Project which uses Maven. Am using Jboss server to deploy.
Problem is file is deployed but when i try to access the URl it says 404 error
url : http://localhost:8080/RESTEasy/rest/RESTEasyHelloWorld/data
There is no error in Console
Following are my files:
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.rest</groupId>
<artifactId>RESTEasy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</version>
</dependency>
</dependencies>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>RESTEasy</display-name>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- this should be the same URL pattern as the servlet-mapping property -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
</web-app>
Rest Java file:
package com.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/RESTEasyHelloWorld")
public class RESTEasyHelloWorldService {
#GET
#Path("/{pathParameter}")
public Response responseMsg( #PathParam("pathParameter") String pathParameter
) {
String response = "Hello from: " + pathParameter;
return Response.status(200).entity(response).build();
}
}
Server Log:
08:51:54,288 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015876:
Starting deployment of "RESTEasy-0.0.1-SNAPSHOT.jar" (runtime-name: "RESTEasy-0.0.1-SNAPSHOT.jar")
08:51:54,300 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565:
Replaced deployment "RESTEasy-0.0.1-SNAPSHOT.jar" with deployment "RESTEasy-0.0.1-SNAPSHOT.jar"
The app needs to be deployed in a war (with the correct webapp structure) not a jar.
If you don't change the final name, the war will have the -0.0.1-SNAPSHOT suffix, which will be needed in the path. You could add <finalName>${project.artifactId}</finalName> to the <build> in the pom and that would just make the war the project name