1

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

Jana
  • 266
  • 3
  • 5
  • 18
  • Ehm, `@Path("test")` and `service.path("bestellung")...` - is that a typo or perhaps your problem? – Anders R. Bystrup Sep 04 '13 at 11:40
  • Hi Anders, no that's neither typo nor the misstake... I forgot to add that my service (resource) class is annotated @Path("bestellung"). So I have to add both to the URL path. First "bestellung", then "test". I'll add that in the post! – Jana Sep 05 '13 at 07:19
  • I'm not getting a 405 using your code in my environment. So double check a few things: 1) you are actually requesting what you think you are (turn on logging with `resource.setFilter(new LoggingFilter(System.out))` to view the outgoing request), 2) you don't have another method with the same path, and 3) you don't have a filter changing your request somewhere. By the way, `list` on the server side will contain a list of the form parameters. In your case it will contain 1 element, a string representation of the list you created on the client `"[1, 2, 3]"` not the list itself. – DannyMo Sep 05 '13 at 15:52
  • Yeah, I found out one misstake! I moved my service from an older server (where POST didn't work at all) to a newer one, and didn't change the service-url in my client @_@ BUT, now, I can send a list os 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? – Jana Sep 06 '13 at 11:52

1 Answers1

0

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

Jana
  • 266
  • 3
  • 5
  • 18
  • The new question you find [HERE](http://stackoverflow.com/questions/18694297). I already found out that objects cannot be passed directly and have to be transferred into XML or JSON Strings before posting. See the link for more information. – Jana Sep 10 '13 at 10:52