make wadl file resteasy without maven - java

i am trying to make Wadl file for rest services in my application , i am using resteasy , all tutorials and examples is to make it with maven & jersy
i dont use maven i use eclipse Wildfly and Resteasy , is there is any explanation how to make this .
thank you.

As of Resteasy 3.0.14-Final this should be possible:
https://issues.jboss.org/browse/RESTEASY-166
https://docs.jboss.org/resteasy/docs/3.1.0.Final/userguide/html/WADL.html
Add this to your web.xml:
<servlet>
<servlet-name>RESTEasy WADL</servlet-name>
<servlet-class>org.jboss.resteasy.wadl.ResteasyWadlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RESTEasy WADL</servlet-name>
<url-pattern>/application.xml</url-pattern>
</servlet-mapping>
I tried this on WildFly 10.1.0 and I had to include an extra library:
Maven artifact: https://javalibs.com/artifact/org.jboss.resteasy/resteasy-wadl
Add the following to your pom.xml:
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-wadl -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-wadl</artifactId>
<version>3.0.14.Final</version>
<exclusions>
<exclusion>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
</exclusion>
</exclusions>
</dependency>
Next re-publish and access the WADL at [context-root]/application.xml

Related

JBOSS not finding my project's Maven Dependency

I am running a MAVEN app inside JBOSS AS 5.1 that has a jersey-server dependency. This is my pom.xml entry for the dependency.
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
<scope>runtime</scope>
</dependency>
Simple, right? Here is my web.xml for the project which points to the Servlet Container that exists within this jar.
<servlet>
<servlet-name>JerseyAPI</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>edu.emory.clinical.trials.api</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JerseyAPI</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Although the server starts up fine, I get a ClassNotFoundException for com.sun.jersey.spi.container.servlet.ServletContainer when I attempt to hit the configured #Path. I'm using Eclipse as my IDE, and I can clearly see the class is generated in the maven repository dependency structure (.m2/repository/com/sun/jersey/...).
I'm a bit new to Maven, so I'm sure there must be something basic I am missing, but it doesn't appear that JBOSS is recognizing my project's runtime dependencies that I've declared in the POM. Any advice would be appreciated.
I believe <scope>runtime</scope> is going to be your problem. Look into the <scope> tags.

Jersey web services

I am trying to set up a simple Java web application using jersey for web services. However I have de following problem.
The tomcat server can’t find the resource http://localhost:8081/OnlineShop/rest/books/list but it can find my simple servlet http://localhost:8081/OnlineShop/index
I have the following web.xml
In the other hand I noticed that com.sun.jersey.spi.container.servlet.ServletContainer is present in my project because I added the dependency using maven however jersey.config.server.provider.packages is not present. Maybe that is the problem but I don’t know the exact dependency which I have to add.
My BookRest.java has the following code and is on the com.shop.rest package.
Finally my pom.xml has the following dependencies.
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Please, please get rid of this whole project. You're obviously a beginner and seem to be just putting random configurations and dependencies together, maybe from different tutorials. Your dependencies are incompatible and your web.xml configuration is wrong. Like i said, scrap the whole project and start from scratch. If you are just beginning, you should use one of the startup apps.
You're in Netbeans, so just do the following
File → New Project
Maven → Project from Archetype
Search jersey-quickstart-webapp
Select the one with the Group ID org.glassfish.jersey.archetypes
The latest version should be displayed.
Should be self explanitory from there
You will that the only dependency added is
<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>
And the web.xml will look something like
<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.stackoverflow.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
This will get you a simple app up and running. You will see a dependency that you need to un-comment for JSON support. Un-comment it. Or better yet, un-comment it, then change jersey-media-moxy to jersey-media-json-jackson. Jackson is IMO a better JSON library.
Also keep the Jersey Documentation handy for some good reading and reference material for working with Jersey

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>

Setting up Jax-rs 2.2 with Jetty

I'm trying to set up some REST services on Jetty using Jersey JAXRS. I can't get json data through to my REST service class though. My ajax requests keep getting the "Unsupported Media Type" error and status. I get this regardless of what #Produces and #Consumes annotations I add to my methods though they should both be MediaType.APPLICATION_JSON.
I can't find decent documentation on Jersey and the loads of questions, blogs, and other resources all seem to be out of date. Looks like Jersey has undergone a lot of changes recently and I'm at a loss as to where I should be looking. I set up the following based on the jersey webapp archetype:
web.xml:
<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.my.package.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Code snippet:
#Path("/users")
public class UserService {
// Plain text works!
#GET
#Consumes(MediaType.TEXT_PLAIN)
public String list(){
return "Got it!";
}
// JSON doesn't work! >:(
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public User create(User user) {
Mocks.USERS.add(user);
return user;
}
My parent pom manages these dependencies ahd the second of these two is a dependency in my jax-rs project pom.
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.2</version>
</dependency>
Do I need something to add support for JSON?
Have you read a chapter dedicated to JSON in the Users Guide? The easiest way would be adding a dependency on MOXy and JSON support would work out-of-the-box (you don't need to explicitly register features the modules provides to make it work as opposed to other JSON modules in Jersey):
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.2</version>
</dependency>
Anyways Jersey provides more modules that would help you with handling JSON media type:
MOXy (examples: json-moxy, bean-validation-webapp)
Jackson (example: json-jackson)
Java API for JSON Processing (JSON-P) (example: json-processing-webapp)
Jettison (example: json-jettison)
Seems Drew was on the right track in his comment. But the answer (for Jersey 2.2 + Jackson at least) was a more up-to-date provider
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
Using this required no configuration. Use this with the two dependencies in the original questions and you're in business.
JSON start working for me just with 2 dependencies:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>

where do I place XHTML files to make them visible in GlassFish web container?

This is the structure of my project (exactly these five files):
/p1
pom.xml
/src
/main
/java
/webapp
a.html
b.xhtml
/WEB-INF
faces-config.xml
web.xml
I'm deploying this WAR to GlassFish and I can successfully access this URL: http://localhost:8080/p1/a.html. When I'm trying to open http://localhost:8080/p1/b.xhtml I'm getting a message
The requested resource (/p1/b.xhtml) is not available.
What am I doing wrong?
ps. My dependencies from pom.xml:
...
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>${facelets.version}</version>
</dependency>
...
This is my web.xml (core part of it):
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
My faces-config.xml:
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
For the Maven side, things looks ok, except that facelets should also be provided. Actually, I use the following dependency:
<!-- This dependency will bring in everything we need for JAVA EE6 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
For the JSF part, nothing in the server logs? Just in case, could you add the following to your web.xml to see if you get more useful output:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
As a side note, you don't need your faces-config.xml as Facelets is the default view handler in JSF 2.0. But this shouldn't be a problem.
PS: Personally, I prefer to map the Faces Servlet on something like *.jsf (to clearly de-correlate any mapped url from the actual .xhtml facelet page that will be processed by the Faces Servlet).
See also
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?

Categories