将 glassfish 3.0.1 升级到 3.1 给出了对 REST ws 的错误请求
我有一个在 Glassfish 3.0.1 上使用 multipart/formdata 参数的 REST Web 服务。由于性能缓慢,我已将服务器升级到3.1。我正在使用 Jersey,为此我在 Webservice 项目中有适当的 web.xml:
<?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>
我已经向 webservice 发出了 ajax 请求。 html 代码是:
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;
}
});
请求在升级 Web 服务器之前和之后都完美地工作,我得到的响应是 400 - 错误请求。 升级后,除了将 @FormParam 更改为 @FormDataParam 之外,我没有进行任何代码更改(因为升级到使用 jdk 7 的 glassfish 3.1 后出现已弃用的错误)。
如果有人知道为什么会发生这种情况,请告诉我。 提前致谢!
编辑: 使用 @FormParam 可以让请求完美地通过。因此,当 @FormParam 更改为 @FormDataParam 时,球衣和我为请求创建的 FormData 对象之间的某些链接会中断。有解决方法吗?
我在球衣中启用了日志记录和跟踪。这是我的输出:
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 <
没有异常,无法找出问题所在。我该怎么办?如果 Glassfish 3.1 包含 jersey multipart jar,那么 multipart 请求不起作用的原因是什么?
请帮我解决这个问题
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案,虽然可能不太优雅,但符合我的目的。
我已将 @comsumes 媒体类型更改为形式 url 编码。在客户端 html 中创建表单标签并将其发布到 Web 服务。 Ajax 调用的数据类型为 application//x-www-form-urlencoded。现在 @formParam 从客户端获取正确的值。
在此之前,我已经尝试过许多变体:-将@FormParam更改为@FormDataParam,然后添加FormContentDisposition部分,但无济于事!在球衣论坛和这里都没有找到解决方案。所以必须继续使用这个解决方案。
如果有人发现它,请更新正确的解决方案。
卡维塔
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