I need to get WADL file for RESTful service. I know that in case using jersey it's available as http://localhost:8080/application.wadl. But I use RESTeasy.
Can I do the same in my framework case?
Latest versions:
Quoting Chapter 49. RESTEasy WADL Support:
Chapter 49. RESTEasy WADL Support
49.1. RESTEasy WADL Support for Servlet Container
49.2. RESTEasy WADL support for Sun JDK HTTP Server
49.3. RESTEasy WADL support for Netty Container
49.4. RESTEasy WADL Support for Undertow Container
RESTEasy has its own support to generate WADL for its resources, and it supports several different containers. The following text will show you how to use this feature in different containers.
49.1. RESTEasy WADL Support for Servlet Container
RESTEasy WADL uses ResteasyWadlServlet to support servlet container. It can be registered into web.xml to enable WADL feature. Here is an example to show the usages of ResteasyWadlServlet in 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>
The preceding configuration in web.xml shows how to enable
ResteasyWadlServlet and mapped it to /application.xml. And then the
WADL can be accessed from the configured URL:
/application.xml
Workaround for Older versions
There is a workaround: a maven plugin called maven-wadl-plugin by the jersey folks that also works to generate WADL for services coded using RESTEasy.
Here's how to use it.
1. Add this to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>maven-wadl-plugin</artifactId>
<version>1.17</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<phase>${javadoc-phase}</phase>
</execution>
</executions>
<configuration>
<wadlFile>${project.build.outputDirectory}/application.wadl
</wadlFile>
<formatWadlFile>true</formatWadlFile>
<baseUri>http://example.com:8080/rest</baseUri>
<packagesResourceConfig>
<param>com.example.rs.resource</param>
</packagesResourceConfig>
<wadlGenerators>
<wadlGeneratorDescription>
<className>com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc
</className>
<properties>
<property>
<name>applicationDocsFile</name>
<value>${basedir}/src/main/doc/application-doc.xml</value>
</property>
</properties>
</wadlGeneratorDescription>
<wadlGeneratorDescription>
<className>com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport
</className>
<properties>
<property>
<name>grammarsFile</name>
<value>${basedir}/src/main/doc/application-grammars.xml</value>
</property>
</properties>
</wadlGeneratorDescription>
</wadlGenerators>
</configuration>
</plugin>
</plugins>
</build>
Pay attention to the baseUri and packagesResourceConfig elements. You have to change them to reflect your project's configuration. You may also want to change the plugin's version (I used 1.17).
2. Create a /doc folder and add some files.
Create the src/main/doc/ folder and create the two files below.
File: application-doc.xml
Content:
<?xml version="1.0" encoding="UTF-8"?>
<applicationDocs targetNamespace="http://wadl.dev.java.net/2009/02">
<doc xml:lang="en" title="A message in the WADL">This is added to the start of the generated application.wadl</doc>
</applicationDocs>
File: application-grammars.xml
Content:
<?xml version="1.0" encoding="UTF-8" ?>
<grammars xmlns="http://wadl.dev.java.net/2009/02" />
3. Run the maven command.
Go to the project folder and run the following command:
$ mvn compile com.sun.jersey.contribs:maven-wadl-plugin:generate
The files \target\classes\application.wadl (the WADL itself) and \target\classes\xsd0.xsd (the schema of the resources - it's used by the application.wadl) should be generated.
Edit and use them as you wish.
PS.: Bear in mind that this is a very simple use of the maven-wadl-plugin. It can do a lot more. To know it better, please refer to the zip file in http://search.maven.org/remotecontent?filepath=com/sun/jersey/samples/generate-wadl/1.12/generate-wadl-1.12-project.zip
WADL generation in RESTeasy is a feature not yet implemented. If you want it go vote for it.
https://issues.jboss.org/browse/RESTEASY-166
See RESTEasy WADL Support (3.1.0). The snipped below is copied from there
<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>
This uses the ResteasyWadlServlet and will make the WADL accessible at /application.xml.
Note:
Rex and Jaskirat have already mentioned previously that RESTEASY-166 was used to manage the implementation for this feature. It seems this was completed in 3.0.14.
we can generate a wadl with the help of maven project with POM.XML
https://issues.jboss.org/browse/RESTEASY-166 check the comments here..!!
Related
I'm trying out Java coming over from .NET and want to make a simple page that does the following:
Implements RESTful API
Connects to a MySQL Database
Implements Role-based Security
I tried out Eclipse but wasn't met with much success, plus the code completion was very slow compared to VS2017. So I'm trying out IntelliJ Community. I've built a Maven project, but my URL is not running, also it looks like I'm not getting any code completion in my .java files. It did not build the app directories automatically.
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.johnnytest.webapp</groupId>
<artifactId>jerseyexample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jerseyexample Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
<build>
<finalName>jerseyexample</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/</url>
<!--<username>joe</username>-->
<!--<password>joe</password>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
My 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>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</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.johnnytest.app</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
My Java file:
package com.johnnytest.app;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/test")
public class MainApp {
#GET
#Path("/{param}")
public Response getMessage(#PathParam("param") String message) {
String output = "Jersey says " + message;
return Response.status(200).entity(output).build();
}
}
Also it's running Tomcat 7, and I've installed 9 on my system. Is there any way to get it to run on my existing Tomcat install? When I go to any URL on localhost:8080 I just get a blank page in Safari. Running on OSX Sierra.
Launch at Command line
if you didn't do it already, download Apache Maven 3.5.0 and add the /bin directory to the system PATH variable.
Enter the directory where the pom.xml file is and run the following:
mvn clean package
mvn tomcat7:deploy
mvn tomcat7:start
The first command will build the code and produce a Java WAR archive in the subdirectory target/.
The second command will deploy the WAR archive to Tomcat application server.
The third command will start Tomcat and expose the server as per configuration (localhost:8080).
Don't worry about the command tomcat7, even if you're using Tomcat 9 it should be fine.
Launch within IntellJ
Maven management in IntelliJ is not really optimal, but on the right side you should see a Maven Projects pane (if you don't, select "Tools -> View tool buttons" menu). Click and expand.
Under Lifecycle you will find the standard Maven goals, so double clicking on clean and then package will produce the WAR file in a similar manner as explained in the previous section.
Under Plugins you will find the Maven goals ruled by the Tomcat plugin. Expand and double click tomcat7:deploy and tomcat7:start.
A quick parallel with ASP.NET
If you come from .NET, just think of Tomcat as the Java version of IISExpress that comes bundled with VisualStudio. It is a application server where you deploy the application code and which emulates a webserver service for development.
There are many parallels and also many discrepancies. In general, an application lifecycle in .NET is much more contained, you rarely need anything else beyond VisualStudio and MSBuild, I think. In Java, many things have been created and evolved through the decades thanks to the community: we have different build systems (Maven, Ant, Gradle), the best tooling is command line and there are multiple ways to achieve the same goal.
For example, you are using the Jersey REST library with an underlying app server below (Tomcat), but you could also deploy to a NIO server (Grizzly). If you use extended frameworks like Spring, you generally don't even worry about these things because switching from one way to the other is just a matter of importing a module in your pom.xml instead of another.
Hope this helped you and gave you some directions.
A Spring web application is just a normal web application using servlet API 3.0.
In servlet API 3.0 web.xml file is optional (most of the time).
I' ve tried not to include web.xml inside my Spring aplication,but somehow it seems to be required even using Servlet 3.0
The only difference between a Spring applications and web apps I've run successfully without writing a web.xml file, is that in Spring filters and servlets are defined inside jars in the lib folder.
Any clues why this happens ?
You can't just exclude the web.xml, as you (at least) need to define which version of the Servlet API are you using.
At the minimum, you'd need this in your 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">
Everything else (servlets, filters, etc.) can be configured with annotations.
Fortunately, you can create completely XML free Spring applications nowadays.
There are various different options for such configs with or without Spring Boot.
With pure Spring you can use AbstractAnnotationConfigDispatcherServletInitializer
With Spring Boot, you can have SpringBootServletInitializer.
This is my Github repository with bunch of projects based on both approaches.
Version of servlets is defined by Servlet API you have on your classpath. E.g.:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
or
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
You also don't specify servlets, as Spring has own implementation of servlet DispatcherServlet.
Filters are registered as beans into Spring context.
Typically when you create a webapp you will access your webapp when you hit the url
<your_IP>/<Project_name>/
Example:
127.0.0.1/MyWebapp/
Question: How do you configure your webapp to run from base URL
Example
127.0.0.1/
Moreover, when the browser navigates to your IP (and not your IP + name of your webapp) you can hit your webpage
Question: Is this a configuration file that needs to be edited in your Application web server?
It depends on your container, but generally you will name your file ROOT.war or specify the context explicitly.
For example, since you've tagged this question with jetty, here is the plugin in my pom.xml for embedded testing using mvn jetty:run. Notice the contextPath element.
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<contextPath>/</contextPath>
<scanIntervalSeconds>5</scanIntervalSeconds>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8081</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
When I deploy to Tomcat, I simply name my file ROOT.war and drop it into the webapps folder. Note that you'll need to move or remove the existing ROOT content from webapps first.
Doing this will allow you to access your application at http[s]://<host>[:<port>]/ with no extra context needed.
Assuming you're using maven to build your application, you can avoid manually re-naming your WAR by specifying the finalName in your pom.xml.
<build>
<finalName>ROOT</finalName>
[...]
</build>
I have packaged a number of composite components in a JAR. However, when using them in another project (using Maven), Netbeans editor puts red error lines under lines which use the composite component, even though the project compiles and runs as expected.
The folder structure for the composite component JAR look like:
compositeComponent.jar
META-INF
faces-config.xml
highcharts-taglib.xml
MANIFEST.MF
web.xml
maven
// maven stuff.
resources
highcharts
Chart.xhtml
Series.xhtml
Tooltip.xml
nz
co
kevindoran
highcharts
example
NZPopulationTrend.class
The highcharts.taglib.xml looks like:
<facelet-taglib version="2.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-facelettaglibrary_2_0.xsd">
<namespace>http://nz.co.kevindoran/highcharts-jsf</namespace>
<composite-library-name>highcharts</composite-library-name>
</facelet-taglib>
[Side note: The faces-config.xml and web.xml are present to allow the 'JAR' to be deployed as a WAR by changing the file extension to WAR (this is to done to run the examples).]
In my current project, I have specify a Maven dependency on the above project like so:
<dependency>
<groupId>nz.co.kevindoran</groupId>
<artifactId>jsf-menu</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In a JSF page, I use on of the composite components like so:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:hc="http://nz.co.kevindoran/highcharts-jsf">
....
<hc:TimeChart title="Price Over Time" xLabel="Date" yLabel="Sold Price (NZD)">
<hc:TimeSeries name="Sold" series="#{cc.attrs.model.priceVsTimeChart.soldSeries}"/>
</hc:TimeChart>
....
</html>
Red error lines appear under all lines above, with message: "No library found for namespace http://nz.co.kevindoran/highcharts-jsf"
How do I get these error lines to be removed? I have seen many Netbeans bug reports for similar issues, but all seem resolved.
This error occurs on Netbeans 7.1, 7.2 and 7.3 (including 7.3.1).
I have absolutely the same problem. In my case it depends on the /src/main/java folder. If it's exist (only in the project and not even in the jar) the project which includes this library shows the "No library found for namespace... "
When i remove the "java" folder it works. But then my backing bean class is missed in the jar...
Tried with Netbeans 7.2 and 7.3, maven 2
Solution:
Generate a second project which contains the Java source files. (called: jsf-lib-java)
In jsf-lib project (your composite component project with xhtml) delete the "java" folder and all *.java sources.
add in the jsf-lib pom.xml following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mycompany.project</groupId>
<artifactId>jsf-lib-java</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/</outputDirectory>
<includes>**/*.class</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
That's it. This will generate a "good" jar file with the required *.class files. So it's possible to "trick" Netbeans.
Now i work with this solution. It's a hack but didn't found a better solution.
Is there a tool which takes a Java File what describes a REST service as a parameter and generates a wadl file out of that.
I had the same problem: was using RESTeasy and wanted to find a way to generate the WADL automatically.
Did some research and came to the solution below.
1. Add this to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>maven-wadl-plugin</artifactId>
<version>1.17</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<phase>${javadoc-phase}</phase>
</execution>
</executions>
<configuration>
<wadlFile>${project.build.outputDirectory}/application.wadl
</wadlFile>
<formatWadlFile>true</formatWadlFile>
<baseUri>http://example.com:8080/rest</baseUri>
<packagesResourceConfig>
<param>com.example.rs.resource</param>
</packagesResourceConfig>
<wadlGenerators>
<wadlGeneratorDescription>
<className>com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc
</className>
<properties>
<property>
<name>applicationDocsFile</name>
<value>${basedir}/src/main/doc/application-doc.xml</value>
</property>
</properties>
</wadlGeneratorDescription>
<wadlGeneratorDescription>
<className>com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport
</className>
<properties>
<property>
<name>grammarsFile</name>
<value>${basedir}/src/main/doc/application-grammars.xml</value>
</property>
</properties>
</wadlGeneratorDescription>
</wadlGenerators>
</configuration>
</plugin>
</plugins>
</build>
Pay attention to the buildUri and packagesResourceConfig elements. You have to change them to reflect your project's configuration. You may also want to change the plugin's version (I used 1.17).
2. Create a /doc folder and add some files.
Create the src/main/doc/ folder and create the two files below.
File: application-doc.xml
Content:
<?xml version="1.0" encoding="UTF-8"?>
<applicationDocs targetNamespace="http://wadl.dev.java.net/2009/02">
<doc xml:lang="en" title="A message in the WADL">This is added to the start of the generated application.wadl</doc>
</applicationDocs>
File: application-grammars.xml
Content:
<?xml version="1.0" encoding="UTF-8" ?>
<grammars xmlns="http://wadl.dev.java.net/2009/02" />
3. Run the maven command.
Go to the project folder and run the following command:
$ mvn compile com.sun.jersey.contribs:maven-wadl-plugin:generate
The files \target\classes\application.wadl (the WADL itself) and \target\classes\xsd0.xsd (the schema of the resources - it's used by the application.wadl) should be generated.
Edit and use them as you wish.
PS.: Bear in mind that this is a very simple use of the maven-wadl-plugin. It can do a lot more. To know it better, please refer to the zip file mentioned in the other answer (by Pavel Bucek).
Yes, please see gerenate-wadl [1] sample from Jersey samples (look for maven-wadl-plugin).
[1] http://search.maven.org/remotecontent?filepath=com/sun/jersey/samples/generate-wadl/1.12/generate-wadl-1.12-project.zip