I have a REST webservice using multipart/formdata parameters on Glassfish 3.0.1. Due to slow performance I have upgraded the server to 3.1. I am using Jersey and for that I have the appropriate web.xml in my Webservice project:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>RestWSGS</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>ws</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/jersey/*</url-pattern>
</servlet-mapping>
</web-app>
I have made ajax requests to the webservice . The html code is :
var formdata = new FormData();
formdata.append("connecturi","jdbc:mysql://localhost/schema1");
formdata.append("username","root");
formdata.append("password","metamagics");
$.ajax(
{
url: 'http://comp1:8080/RestWSGS/jersey/GetSchemaTable',
async: false,
data: formdata,
type: 'POST',
cache: false,
dataType: 'text',
contentType: "multipart/form-data",
processData: false,
success: function(data)
{
dbtb = data;
}
});
The request has worked flawlessly before and after upgrading the web server the response I get is 400 – Bad Request.
I haven’t made any code changes after upgrading except changing @FormParam to @FormDataParam (Since I got a deprecated error after upgrading to glassfish 3.1 which uses jdk 7).
In case anyone has any idea why this is happening please let me know.
Thanks in advance!
EDIT:
Using @FormParam instead allows the request through flawlessly. So Some link between jersey and the FormData object I am creating for the request breaks when the @FormParam changes to @FormDataParam. Is there a workaround for this ???
I enabled logging and trace in jersey. This is my output:
INFO: 6 * Server in-bound request
6 > POST http://comp1:8080/RestWSGS/jersey/GetJson
6 > host: comp1:8080
6 > user-agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
6 > accept: application/json, text/javascript, */*
6 > accept-language: en-us
6 > accept-encoding: gzip, deflate
6 > accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
6 > connection: keep-alive
6 > content-type: multipart/form-data
6 > x-requested-with: XMLHttpRequest
6 > referer: http://comp1:8080/GUIForGS/gridsense/fileSelection.html
6 > content-length: 621
6 > pragma: no-cache
6 > cache-control: no-cache
6 >
-----------------------------187161971819895
Content-Disposition: form-data; name="purpose"
new
-----------------------------187161971819895
Content-Disposition: form-data; name="filename"
BoilerHeater_BQPRJun09.xls
-----------------------------187161971819895
Content-Disposition: form-data; name="username"
abcd#7 11 2011 12 24 23
-----------------------------187161971819895
Content-Disposition: form-data; name="password"
abcd#7 11 2011 12 24 23
-----------------------------187161971819895
Content-Disposition: form-data; name="company"
Third
-----------------------------187161971819895--
INFO: 6 * Server out-bound response
6 < 400
6 < X-Jersey-Trace-000: accept root resource classes: "/GetJson"
6 < X-Jersey-Trace-001: match path "/GetJson" -> "/application\.wadl(/.*)?", "/WriteIntoFiles(/.*)?", "/GetSchemaTable(/.*)?", "/ExcelHtmlTable(/.*)?", "/UploadFiles(/.*)?", "/UpdateDB(/.*)?", "/GetTable(/.*)?", "/GetJson(/.*)?"
6 < X-Jersey-Trace-002: accept right hand path java.util.regex.Matcher[pattern=/GetJson(/.*)? region=0,8 lastmatch=/GetJson]: "/GetJson" -> "/GetJson" : ""
6 < X-Jersey-Trace-003: accept resource: "GetJson" -> @Path("/GetJson") ws.GetJson@18a76d6
6 < X-Jersey-Trace-004: match path "" -> ""
6 < X-Jersey-Trace-005: accept resource methods: "GetJson", POST -> ws.GetJson@18a76d6
6 < X-Jersey-Trace-006: matched resource method: public spreadsheet.Exceldatalist ws.GetJson.getJson(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
6 < X-Jersey-Trace-007: matched message body reader: class com.sun.jersey.multipart.FormDataMultiPart, "multipart/form-data" -> com.sun.jersey.multipart.impl.MultiPartReaderServerSide@a74cfe
6 < X-Jersey-Trace-008: mapped exception to response: javax.ws.rs.WebApplicationException@19b218 -> 400 (Bad Request)
6 <
There is no exception, no way to find out what is wrong. How should I go about this?? If Glassfish 3.1 includes jersey multipart jar, what is the reason the multipart request does not work?
Please help me with this
I have found a solution for this which might not be elegant but serves my purpose.
I have changed the @comsumes mediatype to form url encoded. Created a form tag in the client html and posted it to the web service. Ajax call has dataType as application//x-www-form-urlencoded. Now the @formParam gets proper values from the client.
Before this I had tried out many variations :- changed @FormParam to @FormDataParam then added the FormContentDisposition part but to no avail! found no solution at the jersey forum neither here. So had to move on with this solution.
Please update with proper solution in case anyone finds it.
Kavita