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.