1
votes

I have a Jersey2 service with a POST method handler

@Path("register")
public class RegisterResource {
    @XmlRootElement
    class PostUserParams {
        String username;
        String password;

        public PostUserParams() {}
    }


    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public postData(PostUserParams user) {
    ...
    }
}

But I can't get the POST request to work - I already get 415 Unsupported Media Type. I'm pretty sure that

  • I have the correct Content-Type in the request
  • moxy, jaxb and all the jars and dependencies are properly deployed
  • I even tried to add some moxy configuration for jersey, but that doesn't seem to be necessary according to the docs
  • other requests not involving JSON to Java conversion work fine
  • the JSON in the request is correct

What gives?

1

1 Answers

3
votes

After a lot of digging and debugging (Jersey debugging makes me dizzy!) I found an exception which was unfortunately swallowed inside of these libraries.

The bean class, PostUserParams in this case, must have a parameterless constructer. I have that, but the class is not static and therefore Java doesn't treat it as such.

Solution: make PostUserParams static. Or put it outside the resource class.