Tag Archive: java


So, I am working on this project. It’s written in Java and it consists on several web services (tomcat servlets) interacting. For the client part I am using Jersey. I have been writing a couple of servlets, and they’ve been interacting just fine, until yesterday, I ran into a problem.

The code to make a POST requests consist of the following form:

String myURL="http://localhost:8080/SomeService/someFunction";
UriBuilder uriBuilder = UriBuilder.fromUri(myURL);
FormDataMultiPart form = new FormDataMultiPart();

form.field("Field1", Field1);
form.field("Field2", Field2);

URI location = uriBuilder.build();
WebResource webResource = Client.create().resource(location);
String response= webResource.type(MediaType.MULTIPART_FORM_DATA)
.post(String.class,form);

Code written this form has been working fine until yesterday. Until this happened


SEVERE: Servlet.service() for servlet [SomeServlet] in context with path [/someFunction] threw exception
com.sun.jersey.api.client.ClientHandlerException: javax.ws.rs.WebApplicationException: java.lang.IllegalArgumentException: Missing body part entity of type 'text/plain'

raised by webResource.type(MediaType.MULTIPART_FORM_DATA).post(String.class,form);/

Google wasn’t very helpful solving this issue. The problem: one of the fields had a null value. So basically, this is a NullPointerException. If it said so, I knew what to look for, but… looking at this message, I hadn’t a clue what to look for. I was more-or-less expecting this meant the servlet being called returned nothing. But, after some debugging, including sniffing packets using WireShark, I had to make the conclusion no request was attempted. From there, I started thinking back about my changes. I realised on the the last steps I made was adding more fields to the request. So, I uncommented the fields I’ve added, and it made the (incomplete) request just fine. From there, I realised the issue must be with the newly added fields. That’s how I found out what the problem really was about.

I have recently finished my master thesis project. Now I have been asked to integrate my project with another project my university is running. This project is a distributed Java project. Components of the project are implemented as Java Servlets, which run on a Tomcat server. The communication between the various components is implemented using the Jersey library.

So, the idea is that I implement the various components from my projects as Java Servlets, capable of running on a Tomcat server, using the Jersey library. In order to develop for this target, some set-up is required. First of all, as an IDE, not the regular version of Eclipse, but Eclipse for Java EE developers is required.

Once we have that edition up and running, we can create the set-up required for tomcat. Eclipse can download and install an installation of tomcat in a specified directory. I usually don’t like to install software outside the package manager, but since Eclipse will write configuration files, simply pointing to the system installation of Tomcat is not going to work.

So, let’s have a look at this Tomcat. Start Eclipse in a new workspace, and click the “Workbench” arrow in the top right.

1

Then, click windows -> show view -> servers. Now, a message appears at the bottom saying to servers have been configured. Click the message to configure a server.

2

3

Select Server type “Tomcat v 7.0” (listed under Apache). Please note that I am using Tomcat version 7 as this is the version used by my university. I suppose new projects should use version 8 instead.

4

In the next window, click Download and install, select a directory in your home directory, and wait until the

Finish

button is no longer gray. At the main window, right bottom, a progress indicator will be visible

5

Now, we follow this tutorial, section 6.1 and 6.2. For the jersey library, I’ll use the Jersey 1.18 zip bundle from the jersey website. I am using version 1.18 as this is used by my university. New projects should probably use an 2.x version. The JAR bundle should work as well but does not include all libraries. When using the bundle, the asm library should be downloaded separately.

Now, take a look at section 6.3. This example is written for the glassfish server, however, we are using the tomcat server. Replace org.glassfish.jersey.servlet.ServletContainer by com.sun.jersey.spi.container.servlet.ServletContainer.

Now, when we attempt to start the server, a configuration error message will appear.

7--

8

I am not quite sure what causes it, but when we delete the server and add it again, it will work. Now we’ve got a working Jersey Hello World running within Eclipse JEE.