使用带有@FormParam 的 POST 获取 405“不允许的方法”错误(带有 Jersey REST 的 Java Web 服务)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18611419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:30:27  来源:igfitidea点击:

Getting 405 "Method Not Allowed" error using POST with @FormParam (Java web service with Jersey REST)

javarestpostjerseyform-parameter

提问by Jana

I know it is not easy to pass something to the REST Server (Resource) which is neither String nor a simple Type.
But for a simple ordering process I need to send a list of articles (which shall be ordered) from the client to teh server.

我知道向 REST 服务器(资源)传递既不是 String 也不是简单类型的东西并不容易。
但是对于一个简单的订购过程,我需要从客户端向服务器发送一个文章列表(应该被订购)。

I already tried using "QueryParam", converting my object (I wrapped the list into a DTO) into JSON-String and passing it. It didn't work. (But for other methods which don't need to pass an object to the server my service works fine, even POST methods.)

我已经尝试使用“QueryParam”,将我的对象(我将列表包装成 DTO)转换为 JSON-String 并传递它。它没有用。(但对于不需要将对象传递给服务器的其他方法,我的服务工作正常,即使是 POST 方法。)

Then I found out about the @FormParam which can theoretically transfer every kind of object. (That's what I read, is it actually true?)

然后我发现了@FormParam 理论上可以传输各种对象。(这就是我读到的,这是真的吗?)

So I tried in a very simple test method to pass a List of Strings to the Service, the serverside should give back the number of elements of that list.

所以我尝试了一个非常简单的测试方法,将一个字符串列表传递给服务,服务器端应该返回该列表的元素数量。

That's my code:

那是我的代码:

On Server-Side (Resource):

在服务器端(资源):

@Path("bestellung")
public class BestellungResource {

  @Path("test")
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(XML)

  public Integer test(
    @FormParam("list") List<String> list){

      return list.size();        
  }
}



And on Client Side (in a Session Bean):

在客户端(在会话 Bean 中):

public Integer  test() {

    List<String> list = new ArrayList<String>();
    list.add("1");
    list.add("2");
    list.add("3");

    Form form = new Form();
    form.add("list", list);

    return service
             .path("bestellung")
             .path("test")
             .type(MediaType.APPLICATION_FORM_URLENCODED)
             .post(Integer.class, form);
}

Where serviceis built like that:

服务是建立这样的:

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
service = client.resource(UriBuilder.fromUri("<service url>").build());

Invoking this client method from my GUI or directly via EJB Explorer always gives the 405 error.

从我的 GUI 或直接通过 EJB Explorer 调用这个客户端方法总是会出现 405 错误。

Where's the problem? Did I miss something with POST, the MIME types or the Form?

问题出在哪里?我是否遗漏了 POST、MIME 类型或表单的某些内容?

By the way, even with simple form parameters like a String or int it does not work and throws a 405 error as well.

顺便说一句,即使使用像 String 或 int 这样的简单表单参数,它也不起作用,也会引发 405 错误。

Thanks for your help!
Jana

谢谢你的帮助!
贾娜

采纳答案by Jana

Okay.

好的。

Due to the fact that I found the reason of the 405 error, I'll close this question. But I'll open a new one for the new found misstake of the 400 error. I explained the "solution" in a comment above:

由于我找到了405错误的原因,我将关闭这个问题。但是我会为新发现的 400 错误错误打开一个新的。我在上面的评论中解释了“解决方案”:

I moved my service from an older server (where POST didn't work at all and always threw a 405) to a newer one, and didn't change the service-url in my client! @_@ (so the new client still tried to reach the old server)

But now, I can send a list of Strings, but none of my own class objects (entities or DTOs). Now I'm getting a HTTP 400 error "Bad Request". :-( But I didn't change anything else. Isn't it possible to send not-standard-types?

我将我的服务从旧服务器(POST 根本不起作用并且总是抛出 405)转移到新服务器,并且没有更改客户端中的服务 URL!@_@(所以新客户端仍然试图访问旧服务器)

但是现在,我可以发送字符串列表,但不能发送我自己的类对象(实体或 DTO)。现在我收到HTTP 400 错误 "Bad Request"。:-( 但我没有改变任何其他东西。不是可以发送非标准类型吗?

(--> I'll ask that in a new question.)

(--> 我会在一个新问题中提出这个问题。)

Sorry for this silly misstake.
Jana

对不起这个愚蠢的错误。
贾娜