How to use #XmlVariableNode in Jersey? - json

Found this example, exactly what I want: MOXy's #XmlVariableNode - Using a Map's Key as the Node Name, but tough luck using it in my Jersey 2.2 application.
#XmlVariableNode("key") on MapAdapter.AdapterdMap.entry is giving a compile error:
XmlVariableNode cannot be resolved to a type

Reason being org.eclipse.persistence.oxm.annotations.XmlVariableNode is only available in org.eclipse.persistence.moxy (EclipseLink Moxy) 2.5.1 and 2.6.0, which are only available in a nightly build at the moment.
To make it work with Jersey 2.2 which is using 2.5.0 of EclipseLink Moxy, use the following pom.xml dependencies:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.2</version>
<exclusions>
<!-- To get early access to org.eclipse.persistence.oxm.annotations.XmlVariableNode -->
<!-- TODO get rid of exclusion and use jersey.version=2.3 when it's released -->
<exclusion>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<!--<version>2.5.0</version>-->
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- To get early access to org.eclipse.persistence.oxm.annotations.XmlVariableNode -->
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.1-RC1</version>
</dependency>
and you'll need the SNAPSHOT repository as well:
<repositories>
<repository>
<id>oss.sonatype.org</id>
<name>OSS Sonatype Staging</name>
<url>https://oss.sonatype.org/content/groups/staging</url>
</repository>
</repositories>
Please note that 2.5.1-RC1 is a [more] stable build, alternatively you can use 2.5.1-SNAPSHOT or 2.6.0-SNAPSHOT. For more info dependencies see
http://wiki.eclipse.org/EclipseLink/Maven
https://oss.sonatype.org/index.html#nexus-search;quick~org.eclipse.persistence.moxy
Whenever they release the next version, you can remove the snapshot/RC tag, to watch the news: http://wiki.eclipse.org/EclipseLink; the proposed 2.5.1 date is 27th September 2013
According to Jersey's Road Map 2.3 is coming out 23th September 2013, so it is impossible to include 2.5.1..., so maybe 2.4, until then ... wait for it ... dependency exclusion.

Related

Replace hsqldb with MySQL

I have a Spring REST project configured with hsqldb.
I would like to change it to MySQL.
I have MySQL server installed and running, but I don't know how to modify this pom:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
PS.: Project im talking about comes as a source code for 'Spring REST' book:
http://www.apress.com/9781484208243
source code download link:
http://www.apress.com/downloadable/download/sample/sample_id/1704/
As far I see you are using Spring Boot on this, so you can easy change the databases changing the drivers dependencies from:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
To
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
The driver version will be version on parent pom.
Then specify the parameters on properties
spring.datasource.url=jdbc:mysql://localhost:port/yourdb
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # we can ommit this if we want, Spring Boot will deduce based on the classpath
For more configuration on databases you can see the properties available on appendix here

using primefaces extensions (inputNumber)

i'm new in primefaces word , and i need to use primefaces extensions (inputNumber)
in the XHTML file I add the taglib :
xmlns:pe="http://primefaces.org/ui/extensions"
when adding the jar :
primefaces-extensions-0.6.3;
I have an error : La ressource demandée n'est pas disponible.
When removing it, the application works but the inputNember doesn't show and I get this
error:
Warning: This page calls for XML namespace http://primefaces.org/ui/extensions declared with prefix pe but no taglibrary exists for that namespace.
To work with primefaces extensions i should add to the XHTML file the taglib:
xmlns:pe="http://primefaces.org/ui/extensions"
and to the lib folder two jars:
primefaces-extensions-0.6.3.jar
common-lang3.jar.
that is all.
If you are using maven for your dependencies, the required entry is
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>0.6.3</version>
</dependency>
This will add the required dependencies for primefaces extensions.
If you are not using maven, then you should follow the Getting Started guide for "other users" and add all the necessary jars
For me, one maven dependencies is missing !
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
Attention: currently "commons" is with S at end
At 2016.12.24, the version of extension can now be 6.0.0
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>6.0.0</version>
</dependency>
Thanks to Rong Nguyen and Ghizlane La
Last remark
I have encouter some problems (error message in Chrome indicating that some ressources are missing) because the versions used for Primefaces and PrimefacesExtension end Commons-lang3 are not compatible !
But I have found that the following combination work correclty for me.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>6.0.0</version>
</dependency>
I hope that will be useful for others :-)

Can I install the mysql jdbc connector using mvn install:install-file?

I'd like to install the JDBC connector using maven.
I have the following: mvn install:install-file -DgroupId=mysql -DartifactId=mysql-connnector-java -Dversion=5.1.6 -Dpackaging=jar -Dfile= -DgenerationPom=true
I think all I'm need is what I put on the other side of the =Dfile= ?
I haven't used maven in a while either, so I'm not sure what the file switch is used for?
Thanks for all the insight!
The "install-file" or "deploy-file" goals are used for installing or deploying artifacts to your local or internal repository that are not available from Maven Central or other external repositories that you may have configured.
If you've got access to Maven Central, simply adding the following to your project's pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
...should do the trick.
To answer your question though, the -Dfile= argument is for specifying the artifact that would actually be installed in the local repository.
lotz answer is right and that should be sufficient
But, If you want to use the latest version of the connector, you can check https://mvnrepository.com/artifact/mysql/mysql-connector-java
Add the following to your project's pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>

Set JSON provider at RESTEasy on JBoss 7.1.1

How could I set JSON provider for RestEasy at JBoss 7.1.1?
RestEasy documentation says:
RESTEasy allows you to marshall JAXB annotated POJOs to and from JSON.
This provider wraps the Jettison JSON library to accomplish this.
But I found that it seems that on JBoss 7.1.1 Resteasy uses Jackson provider because #XmlTransient on my class field was ignored, but #JsonIgnore was processed.
How can I tell to Resteasy to use Jettison instead of Jackson?
On Jboss I found both providers.
if you just want to have a different module pulled than the standard, you can provide this in a jboss specific deployment descriptor.
Read https://docs.jboss.org/author/display/AS71/Class+Loading+in+AS7 to learn the details, and read https://docs.jboss.org/author/display/AS7/Implicit+module+dependencies+for+deployments to learn what modules JBoss uses by default.
To exchange the two providers, provide a META-INF/jboss-deployment-structure.xml with the following content below.
This switched the provider for me.
Br Alexander.
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jettison-provider" />
</dependencies>
</deployment>
</jboss-deployment-structure>
As a follow-on to ahus1's answer: if you have a multi-level deployment archive (i.e. a top-level EAR file containing an EJB jar and a war file), you will need to configure the exclusion of org.jboss.resteasy.resteasy-jackson-provider on whichever sub-deployment contains the RESTEasy/JAX-RS components.
In my deployment, for example, my REST endpoints were annotated on top of EJBs in my EJB jar file, while my #ApplicationPath-annotated javax.ws.rs.core.Application subclass which activates RESTEasy was in my war file. I found that the approaches of putting the exclusions solely on the top-level (EAR) deployment (as in ahus1's example) or on the EJB-jar-level deployment were ineffective, but I finally got RESTEasy to use Jettison rather than Jackson by putting the exclusions on all 3 levels (EAR, EJB, Web):
<?xml version="1.0" encoding="UTF-8" ?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jettison-provider" />
</dependencies>
</deployment>
<sub-deployment name="MyApp-ejb.jar">
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jettison-provider" />
</dependencies>
</sub-deployment>
<sub-deployment name="MyApp.war">
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jettison-provider" />
</dependencies>
</sub-deployment>
</jboss-deployment-structure>
It's very likely that I only needed the exclusions on the subdeployments, and putting it on the main EAR deployment is superfluous; I didn't try it that way, since for now putting it on all three seems to work perfectly well.
From what I observed right now, Jackson is the default in JBoss AS 7.1.2.
First, the RestEasy modules are hidden from app's classloader, which IMO should not be.
So I just filed https://issues.jboss.org/browse/AS7-5605 .
Second, to your question: To set the particular provider, you need to remove it from classloader's spot in AS - so again, to go module.xml's and comment out those providers which you don't want to use - if Jackson is available, RestEasy uses it; otherwise it uses Jettison.
Also, add them your project as a compile time dependency, so you can use their specific annotations. Example:
<!-- RestEasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>2.3.4.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
<scope>provided</scope>
</dependency>
Note: Until AS7-5605 is done, you need to set the versions manually.
After (in later versions of AS), you have to remove these versions and use those defined in JBoss BOM. See JBoss AS QuckStarts for example.
Feel free to create and contribute a QuickStart of RestEasy using alternative provider.
I added this line to standalone.conf.bat/sh and it solved my problem.
set "JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager=true"

jetty-env.xml with DataSource leads to failing WebAppContext on mvn jetty:run

I have a really simple webapp project with maven and jetty that has been working very well until now. But now I need to setup MySQL connection pooling with JNDI as the database connections always time out.
First of all here is the relevant content of my pom.xml:
<modelVersion>4.0.0</modelVersion>
...
<packaging>war</packaging>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty-version>8.1.0.v20120127</jetty-version>
</properties>
<dependencies>
...
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
...
</build>
Now I created a jetty-env.xml in the folder /src/main/webapp/WEB-INF with the following content:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="project-db" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/db</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="url">jdbc:mysql://www.example.com:3306/mydb</Set>
<Set name="username">dbuser</Set>
<Set name="password">dbpass</Set>
</New>
</Arg>
</New>
</Configure>
But the problem is that I can't even test if this connection works as the jetty-maven-plugin fails to start on the goal
mvn jetty:run
with the following error:
WARN:oejw.WebAppContext:Failed startup of context o.m.j.p.JettyWebAppContext
{/,file:/D:/documents/programmierung/workspace/battleships-trunk/src/main/webapp/}
,file:/D:/documents/programmierung/workspace/battleships-trunk/src/main/webapp/
java.lang.IllegalArgumentException: Object of class
'org.mortbay.jetty.plugin.JettyWebAppContext' is not of type
'org.eclipse.jetty.webapp.WebAppContext'.
Object Class and type Class are from different loaders.
So how can I get this to work? I'm forced to use Jetty version 8.x as I need WebSocket support and as the remote productive server will be running Jetty 8.
EDIT
Before Pavel Veller's answer I tried the following: Deployed the assembled war to the remote jetty8 server and got the same error only that the previous error now reads as follows:
java.lang.IllegalArgumentException: Object of class
'org.eclipse.jetty.webapp.WebAppContext' is not of type
'org.eclipse.jetty.webapp.WebAppContext'.
Object Class and type Class are from different loaders.
So it seems as if there are multiple class loaders conflicting.
EDIT2
As requested by Pavel I recreated the error with a simplified webapp which you can find here on Dropbox. It is a zipped eclipse maven project.
Try removing the dependency on jetty-maven-plugin- this dependency adds the plugin to the WAR, which you don't want.
If you need to use any classes from Jetty itself, add a dependency for the specific version of Jetty (rather than the plugin) with a scope of provided.
It looks like it's pulling jetty 6 from somewhere. The exception you're seeing seems to be coming from the code that parses jetty-env.xml (org.mortbay.jetty.plus.webapp.EnvConfiguration). The XMLConfiguration class compares the class you declare on the Configure element with the actual class of what it gets from getWebAappContext(). The latter is instance of org.mortbay.jetty.plugin.JettyWebAppContext in your case and you expect it to be org.eclipse.jetty.webapp.WebAppContext (which would be the parent class for JettyWebAppContext had they both come from the same "namespace").
It's hard to tell where that would be happening from but maybe inspect your .m2 and confirm you have the proper binaries for your jetty dependencies? It has got to be running not the version you expect it to run.
UPDATE. Jetty does the following when it loads the classes defined in the configuration:
first load with Thread.currentThread().getContextClassLoader() and
loop through all getParent() until all options are exhausted.
if not successful, attempt to load with the class loader that loaded
jetty core classes (XmlConfiguration.class.getClassLoader())
looping through all the parents as well.
If still not successful, do a Class.forName()
Report ClassNotFoundException if not successful.
you can see it in the code of org.mortbay.util.Loader(http://grepcode.com is a great resource for a quick look under the hood)
It does load the class in your case, but apparently not with the right class loader.
I would now assume you have an extra jetty JAR somewhere on your classpath that interferes with the order of things.
Had a same issue caused by :
<useTestClasspath>true</useTestClasspath> (true instead of false)
That put a extra jetty jar in the classpath...
Including the dependency scope solved the error for me.
<scope>provided</scope>
In the pom.xml it looks like this,
<!-- JETTY DEPENDENCIES -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
in the jetty dependencies and the errors went off. And btw, the jetty version I'm using is 9.3.7.v20160115.
I had the same issue and fixed it but can't figure out why.
By changing
org.eclipse.jetty.webapp.WebAppContext
to
org.eclipse.jetty.maven.plugin.JettyWebAppContext
it started to work for some reason, can't figure out exactly why. Clearly maven plugin has something to do with it?