Validating Null and Empty Strings using Jersey - rest

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).

Related

What is Eclipse referring to with the warning "No JAX-RS Activator is defined for the project"

Eclipse is warning "No JAX-RS Activator is defined for the project".
What does it mean?
Note: This is not a duplicate of the question How to get rid of "No jax-rs activator is defined for the project" warning?
- I don't necessarily want to remove it from displaying. I want to understand it.
I'm guessing you have the JBoss tools installed.
The JaxRsActivator class initializes JAX-RS without the need to use a web.xml file. This is achieved by extending the Application class and using the #ApplicationPath annotation. The value provided in the #ApplicationPath annotation defines the root path for all other JAX-RS HTTP calls.
The second answer to the linked question, which turns off the feature rather than the validation, would seem to apply.

Deployment Descriptor is not updating?

I have installed a brand new Eclipse Java EE version. But my web.xml is not updating automatically as it should while creating a Servlet. Initially it just has the welcome tags, that is all.
This could be because you are using the Servlet 3.0 specification. web.xml mapping is not required for it. Check whether you have the WebServlet annotation in your servlets. It should do the required thing.
Servlet 3.0 uses annotations for URL mapping and not the web.xml servlet configuration tags.
E.g. If you are creating a Servlet called HelloServlet then in your HelloServlet, you will see the WebServlet annotation in it.

How to reference external JPA/JAXB domain model project?

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...

Emply EJB and Servlet

After reading about it for so long, I now have chance to get my hand dirty with EJB. I use Glassfish+Eclipse 3.7 on Ubuntu.
I first created an EJB that just returns a greeting message. Then I create the application client to access this EJB using InitialContext. This works exactly like expected.
Then, I created a servlet to access to that EJB. Neither access with #EJB nor InitialContext works.
When I use #EJB, the 404 page appear with this description: "The requested resource () is not available."
When I use InitialContent, an ClassNotFoundException is thrown. Apparently, the class loader of the servlet cannot access to the EJB class. I tried to add EJB jar file to the servlet's lib folder and I got the error message that the JNDI name already exists. Apparently, Glass Fish tries publish the EJB in the Servlet's lib folder too.
The only way to get this to work is to publish the EJB with the servlet. This way, both I can get the servlet and a stand-alone application to access to that EJB. The problem is that I need to always employ the servlet with the EJB which is not desirable since my client may not want to use web front end.
Anyway, my question is what is appropriate way to have the servlet access to the EJB employed outside its class loader without repeatedly publishing the EJB.
P.S. It is also possible that the problem might be the way Eclipse configure and employ those components.
Thank a lot for any helps.
Perhaps you need to treat the EJB component as if it were remote. And maybe it really is since you don't give a lot of detail on how you are deploying. Try the directions at http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#nonJavaEEwebcontainerRemoteEJB.
A few pointers:
you may need to put the webapp and the ejb-jar in an .ear (enterprise application) and deploy it to glassfish
you may need the remote interfaces on the classpath of the webapp (if they are not available at runtime, but they were at compile time, you can't expect it to work)
NetBeans is generally better with enterprise stuff and wizards for creating and deploying applications. Give it a try.
After try out a while, I found that I can do by referring it as "/". This even works with injection.

No web.xml in Eclipse + Glassfish v3?

I created a simple "hello world" servlet in Eclipse (helios) + Glassfish v3. I am using Glassfish's plugin for eclipse It seems there is no web.xml but a sun-web.xml in the WEB-INF/ folder. This is my first time with glassfish but was a bit surprised at the absence of web.xml - so here are some of the problems:
Where do I check for url-mappings for the servlet? On creating a new Servlet in Eclipse it asks me for a URL-mapping but I'm unable to find it anywhere in any .xml file where I can tweak the settings.
If there isn't any web.xml, creating it from scratch will be quite error prone. What do you suggest? Google for a sample and play around? Shouldn't one be auto-created?
Has anyone encountered this? I tried looking up the difference between web.xml and sun-web.xml but the results weren't at all enlightening. I wouldn't want to learn another xml for configuration purposes and that too glassfish specific.
We have to configure servlet contexts, mappings etc especially during development/testing but the sheer absence of web.xml has me stumped.
Eclipse allows you to not create a web.xml file when you create Dynamic Web Project for Java EE 6, since the Java EE 6 spec (in general) and Servlet 3.0 spec (in particular) attempt to de-emphasize deployment descriptors.
You can use annotation to provide all the data that had been included in the web.xml file. The Javadoc for the Servlet 3.0 annotations is pretty obtuse. You should read through the Servlet 3.0 spec from the jcp.org site to get a bit more explanatory text.
To change the url-mapping for a Servlet 3.0 servlet, the first place to look is in the source code for the servlet. Look for (and change) the value of the urlPatterns element.
If you are trying to create a web app based on Servlet 3.0, try to avoid creating a web.xml file.
The sun-web.xml/glassfish-web.xml file is used to 'finish' the description of a war file for deployment into a GlassFish container.
One other note about the annotations like WebServlet... they do not integrate your annotated class into the class hierarchy, so the correct use of #WebServlet would look like
#WebServlet(
name = "MyServlet",
urlPatterns = {"/path_to_servlet"}
)
public class MyServlet extends HttpServlet {}
If you find you do need a web.xml file, you can context-click on the deployment descriptor in the Project Explorer view and there should be an option "Generate Deployment Descriptor Stub". That will create a web.xml for you with the display-name and welcome-file-list elements.
It seems that it's a bad habit to click "Finish" when you create a 'New > Dynamic Web Project' - You should keep clicking 'next' and go the last window-pane where you select "generate web.xml deployment descriptor" - seems it's unchecked by default.
Well that says I've been a bit rusted with creating web-apps. And here I thought it was a glassfish specific issue.
Because of Glassfish 3.x is fully certified Java EE 6 server, it supports Servlets 3.0. Starting from Servlets 3.0, it is possible to specify web.xml settings through annotations.
For example
#WebServlet(
name = "MyServlet",
urlPatterns = {"/path_to_servlet"}
)
public class MyServlet {}
To add to what TMN said, I noticed that the project explorer would not show the Deployment Descriptor until I performed an SVN update for some reason. If you have that problem, try updating your code. I was on the HEAD revision already but for some reason the update showed that view.