Java Jersey:以字节数组形式接收表单参数
是否可以使用 Jersey 以字节数组形式接收表单参数?
我尝试了以下操作:
@Path("/someMethod")
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String someMethod(@FormParam("someParam") byte[] someParam)
{
return "";
}
但出现此错误:
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String SomeClass.someMethod(byte[]) at parameter at index 0
SEVERE: Missing dependency for method public java.lang.String SomeClass.someMethod(byte[]) at parameter at index 0
SEVERE: Method, public java.lang.String SomeClass.someMethod(byte[]), annotated with POST of resource, class SomeClass, is not recognized as valid resource method.
如果我将 byte[] 更改为 String,则一切正常。
我需要以 byte[] 而不是 String 的形式接收数据的原因是因为数据可能使用不同的字符集进行编码。这取决于提交数据的 HTML 文档,我需要在服务器端正确解码数据(编码字符集在单独的参数中提交)。
所以,如果我可以接收 byte[] 形式的数据,那就可以解决我的问题。也欢迎任何其他解决方案。
谢谢你!
Is it possible to receive form parameter as byte array with Jersey?
I tried the following:
@Path("/someMethod")
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String someMethod(@FormParam("someParam") byte[] someParam)
{
return "";
}
But got this error:
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String SomeClass.someMethod(byte[]) at parameter at index 0
SEVERE: Missing dependency for method public java.lang.String SomeClass.someMethod(byte[]) at parameter at index 0
SEVERE: Method, public java.lang.String SomeClass.someMethod(byte[]), annotated with POST of resource, class SomeClass, is not recognized as valid resource method.
If I change byte[] to String, everything works correctly.
The reason I need to receive data as byte[] and not as String is because data may be encoded using different charsets. It depends on the HTML document that submits data and I need to decode data correctly on server side (encoding charset is submitted in a separate parameter).
So, if I can receive data as byte[], it will solve my problem. Any other solutions are welcome as well.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 Jersey 符合 JAX-RS 规范,则该参数可以是
因为它实际上是在 Jersey API。
如果您想最好地使用 @FormParam,您可以定义一个 ByteArray 类来处理由 String 转换引起的错误并将其用作参数类型。
If Jersey conforms to JAX-RS spec then the parameter may be
as it is actually defined in the Jersey API.
If you want to use @FormParam the best you might be able to do define a
ByteArray
-class that handles the errors caused by the String conversion and use it as the parameter type.谢谢您的回答!我终于找到了解决方案...
现在我看到了一个解决方案,我明白我没有很好地描述我的问题,它会引导您走向不同的方向...
问题是我从使用不同编码的许多不同页面向服务器提交表单数据。
当页面使用 utf-8 编码时,一切正常,但当页面使用不同编码时,特殊字符会丢失。
解决方案是将
accept-charset="utf-8"
添加到谢谢你!
Thank you for your answers! I finally found a solution...
Now that I see a solution, I understand that I didn't describe my problem well enough and it lead you to a different direction...
The problem was that I submit form data to the server from many different pages which use different encodings.
When page uses utf-8 encoding, everything worked correctly but when page uses different encoding, special characters got lost.
The solution was to add
accept-charset="utf-8"
to the <FORM> html element, which caused browser to always encode form data to utf-8 encoding, which solved encoding problem on the server side.Thank you!