Java Jersey:以字节数组形式接收表单参数

发布于 2024-11-14 15:54:04 字数 1032 浏览 4 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-11-21 15:54:05

如果 Jersey 符合 JAX-RS 规范,则该参数可以是

  1. 原始类型
  2. 有一个接受单个字符串参数的构造函数
  3. 有一个名为 valueOf 的静态方法,它接受单个字符串参数
    (例如,参见
    Integer.valueOf(String))
  4. List、Set 或 SortedSet,其中 T 满足上述 2 或 3。这
    生成的集合是只读的。

因为它实际上是在 Jersey API。

如果您想最好地使用 @FormParam,您可以定义一个 ByteArray 类来处理由 String 转换引起的错误并将其用作参数类型。

If Jersey conforms to JAX-RS spec then the parameter may be

  1. A primitive type
  2. Have a constructor that accepts a single String argument
  3. Have a static method named valueOf that accepts a single String argument
    (see, for example,
    Integer.valueOf(String))
  4. List, Set or SortedSet, where T satisfies 2 or 3 above. The
    resulting collection is read-only.

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.

草莓味的萝莉 2024-11-21 15:54:05

谢谢您的回答!我终于找到了解决方案...
现在我看到了一个解决方案,我明白我没有很好地描述我的问题,它会引导您走向不同的方向...

问题是我从使用不同编码的许多不同页面向服务器提交表单数据。
当页面使用 utf-8 编码时,一切正常,但当页面使用不同编码时,特殊字符会丢失。

解决方案是将 accept-charset="utf-8" 添加到

中。 html元素,导致浏览器始终将表单数据编码为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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文