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

Jana picture Jana · Sep 4, 2013 · Viewed 10.4k times · Source

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.

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.)

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

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):

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 service is 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.

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

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

Thanks for your help!
Jana

Answer

Jana picture Jana · Sep 9, 2013

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:

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?

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

Sorry for this silly misstake.
Jana