How to reference external JPA/JAXB domain model project? - eclipse

I am developing a client/server application currently, where the server consists of a RESTful interface (jersey) and the client is a JSF application. Both are running on a glassfish 3.1.1 server. To persist some data and produce XML output I created a domain model with JAXB and JPA (eclipselink) annotations. Everything is fine, as long as the domain classes are within the server project.
But I want to define the domain model in an external project, so that it can be used by the client (xml -> object) and the REST server (object -> xml) by referencing its *.jar.
I alread achieved, that the JPA works correctly on the server, but the JAXB functionality has been "removed".
I am very new to the Java EE stack.. maybe I am packaging in a wrong way. Would be very happy, if someone could give me a hint :-)

Ok, it worked after setting the following project facets:
Java
JAXB
JPA
Uitility Module
After that: adding to build path and setting a project reference...

Related

What to put into jta-data-source of persistence.xml that works on all vendors?

Similar to What to put into jta-data-source of persistence.xml? and How to map jpa datasources in WildFly?
However, I am asking for something that would work on all vendors or at least WildFly, Glassfish/Payara, WebSphere Application Server classic, WebSphere Application Server Liberty, TomEE. I am not looking for something that works in a Java SE Unit test.
So far I found that java:comp/env/jdbc/xxx works in WebSphere Application Server and TomEE. There's a mapping exercise (which is expected) to get it working but I cannot get the same to work on GlassFish/Payara and JBoss/WildFly.
More specifically I do not wish to use default data source because for my scenario I am actually working on two different data sources. E.g. for reference data and another for transactional.
If all of the app servers you work with are Java EE 7 compliant, you can use the default data source, which is required per EE7 spec to be available at:
java:comp/DefaultDataSource
The app server you run on ought to let you customize the configuration of the DefaultDataSource.
Since I'm familiar with WebSphere Liberty, I can point you to this doc for default data sources on Liberty:
Configuring a default data source
If you are using WebSphere traditional, as of v9.0 it supports Java EE 7, and has a default data source available out of the box (under the spec mandated JNDI name).
If you want to use the same JNDI name that works on all servers, it's best to use resource references, as explained in What is resource-ref in web.xml used for?
Basically, you would define an arbitrary JNDI name (ideally without any java:comp prefix or similar, just something like "myDatasource") and then map it to the concrete JNDI name provided by the target server.You would need to define a server-specific descriptor for each server with the mapping the if the server cannot use the JNDI directly (e.g. glassfish-web.xml for GlassFish/Payara, jboss-web.xml for WildFly, ibm-web-bnd.xml for WebSphere Classic and Liberty). TomEE seems to support references without any prefix, so it should be able to configure a datasource without any additional mapping if you choose a name without a prefix.

Validating Null and Empty Strings using Jersey

Can I have a web deployment descriptor file, web.xml, with a Jersey project that runs on top of a Grizzly container? I want to constrain my resource to ensure that input is provided, using #NotNull.
Some context...
I'm using Jersey 2.19 to implement a REST API.
Following the 'Getting Started' section of the user guide I successfully created a new Jersey project that runs on top of a Grizzly container using a Jersey-provided maven archetype.
I have also successfully implemented some bean validation by annotating a resource with various built-in constraints.
I would like add a #NotNull constraint to my resource and for it to mean that input is required - i.e. for an empty string to fail this validation constraint.
The Java EE tutorial refers to making a change to web.xml but there isn't one in my project. I see from the user guide that if I'd created a JavaEE web application instead then the web.xml file would be present.
Can I add one? Or is there another way to validate empty strings in the way I want?
UPDATE
Chapter 18 of the Jersey User Guide does not tell me what I need to know. The Java EE tutorial indicates that I need to set javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in the web.xml file. What is the equivalent of that if I don't have a web.xml?
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is a JSF parameters so it will have no affect on Jersey. Since The default implementation for bean validation with Jersey is Hibernate Validator, you can use it's annotation #NotEmpty. It should serve similar purpose as the JSF parameter (to fail on empty strings).

Is possible to make a library jpa based?

I'm looking to create some projects with common classes for every other project I create, web or standard.
In eclipse I'd already created two projects with the maven quickstart archetype without the jpa facet, but with the eclipselink libraries in the maven POM to anotate entities and jpa stuff. One project is for generic JPA access and another project for security (user entities, user services, user repository) that uses the JPA access project.
Then I create a 3rd project with the same archetype from last 2 project for testing the previous 2, but this have the JPA facet and the Persistence.xml. When I try to do something JPA related, it says the metamodel is empty. Then I found on the internet and the documentation says I have to use the tag in my persistence.xml, but I dont know how since Im including the previous two project in the build path of eclipse, not exactly any jar file. How can I achieve this?
Excuse my english translation.
You probably need an Composite Peristence Unit. Also, it will probably require some care in your built/deployment scripts.

Separate JSF and EJB apps

How do I use a deployed EJB app from a separate JSF application?
I'm attempting to separate the two applications and access the EJB through the remote interface. To do this I have two eclipse projects - one contains the EJB and persistence logic, tested independently and works. I then created a JSF project that references the EJB project (so I gain access to the remote interface), however this fails when attempting to either inject the EJB instance or lookup the JNDI name (I've tried several variants to no avail). This is what my JSF backing bean contains:
#EJB(lookup="java:global/LocEJB/LocalityEJB!com.ame.business.LocalityEJBRemote")
private LocalityEJBRemote locality;
This is on Glassfish, and I am only referencing the EJB project and not packaging it with the JSF project. When I do the latter, I receive error initializing EJB container problems on the JSF project. So, how do I access the remote EJB and does the way I'm approaching this make any sense?
Thanks in advance!
Your JSF application has to know about the EJB interfaces (at least they did on EJB 2.0). You're using the Proxy pattern to hide the fact that this is a remote component from your JSF client.
First of all you can not use Local interface if trying to access outside the container. You must use Remote Interface.
You can define your Remote interface in the sun-web.xml or EJB injection in the bean.
sun-web.xml code:
<ejb-ref>
<ejb-ref-name>com.xxx.session.UserRemote</ejb-ref-name>
<jndi-name>corbaname:iiop:127.0.0.xxx:3700#com.xxx.session.UserRemote</jndi-name>
</ejb-ref>
Another thing you must have Remote interfaces in your classpath.

WSDL Generation Tools

Can anyone recommend a good (preferably open source) tool for creating WSDL files for some soap web services?
I've tried playing around with some of the eclipse plug ins available and was less than impressed with what I found.
As mentioned above, probably the easiest thing to do is use Apache CXF or Apache Axis2 to automatically generate your WSDL for you.
If you have downloaded the Java EE version of Eclipse, you should be able to create a Dynamic Web Project with the Axis2 facets. If you create a simple Java class in the project, you should be able to right-click on it, and choose Web Services->Create Web Service. That should automatically create an Axis2 service for you.
WSDL would then be available from some URL like: http://localhost/axis/{yourservice}?WSDL
One of the more interesting tools for bypassing all the associated headaches with WSDL is the XSLT script created by Arjen Poutsma (the lead developer of Spring Web Services):
http://blog.springframework.com/arjen/archives/2006/07/27/xslt-that-transforms-from-xsd-to-wsdl/
Basically it allows you to develop simple schemas that correspond to your desired operations (i.e. <BuyItem> and <BuyItemResponse>) and then generate all the associated WSDL crap from the XSD. I highly recommend it if you are interested in 'contract-first' web-services but the idea of using a WSDL as the starting point for that contract makes you feel green.
I am tired of generating massive amounts of files on the filesystem just to transport over SOAP. Now I use Apache CXF for both WS producers and consumers and let it handle the WSDL/stubs generation dynamically.
Depends on which language you're working in, but if you're active in Java then I'd recommend looking at Apache CXF. It's a pretty solid framework for publishing java code as a SOAP web service. It also includes a tool for directly generating WSDL files: java2wsdl
Nice tool can be found as SAAS solution at www.cofiq.com. Its strong point is the datamodel repository from which WSDL and REST JSON can be generated and impact analysis on datamodel changes.